<?

/***************************************************************************
 Jupiter Content System @ Jupiterportal.com
 Copyright (C) 2005 Cosmin Flavius (highstrike@gmail.com)

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 More Info About The Licence At http://www.gnu.org/copyleft/gpl.html
****************************************************************************/

//-----------------------------------------------------------------
//  If user is guest and restriction is turned on
//-----------------------------------------------------------------

if($config['forum_guest_see'] == "0" && isset($is_guest))
{ header("location: $PHP_SELF?i=2"); exit; }

//-----------------------------------------------------------------
//  Start Navigation
//-----------------------------------------------------------------

switch($a)
{
	//-----------------------------------------------------------------
	//  Show Forum
	//-----------------------------------------------------------------

	case 1:

		//-----------------------------------------------------------------
		//  Load MYSQL Table + Breadcrumbs
		//-----------------------------------------------------------------

		$forum_cat = $db->getLine("SELECT id, name FROM forum_cat",$db->escape($d));
		if(!$forum_cat) header("location: $PHP_SELF?i=error");

		echo "<table class='main' cellspacing='1' cellpadding='4'><tr class='con1'><td><a href='$PHP_SELF?n=modules/forum'>{$config['forum_name']}</a> &raquo; {$forum_cat['name']}</td></tr></table><br>";

		//-----------------------------------------------------------------
		//  Start display
		//-----------------------------------------------------------------

		?>
			<table class='main' cellspacing='1' cellpadding='4'><tr class='head'><td colspan='5'><img src='templates/<?= $template ?>/images/misc/nav.gif' border='0'> <?= $forum_cat['name'] ?></td></tr>
			<tr class='bottom'><td width='54%' colspan='2'><?= $language['Forum view forum desc'] ?> </td><td width='8%' align='center'><?= $language['Forum view forum desc2'] ?></td><td width='8%' align='center'><?= $language['Forum view forum desc3'] ?></td><td width='30%'><?= $language['Forum view forum desc4'] ?></td></tr>
		<?

		//-----------------------------------------------------------------
		//  Get subcats
		//-----------------------------------------------------------------

		$results = $db->getTable("SELECT s.id AS sid, s.name, s.succession, s.type, s.description, s.topics, s.replies, s.lastpost_date, s.lastpost_email, s.lastpost_user, s.lastpost_id, s.lastpost_location, s.lastpost_reg, u.id AS uid FROM forum_subcat AS s LEFT JOIN users AS u ON s.lastpost_id=u.id WHERE s.cat = '".$db->escape($d)."' ORDER BY s.succession ASC");

		//-----------------------------------------------------------------
		//  Start the foreach
		//-----------------------------------------------------------------

		foreach($results as $idx=>$result)
		{
			//-----------------------------------------------------------------
			//  Check see what indicator fits
			//-----------------------------------------------------------------

			if($result['type'] == "5") $forum_indicator = "<img src='templates/$template/images/buttons/redirected.gif' alt='{$language['Forum indicator desc9']}'>";
			else
			{
				if(@in_array(array("{$result['sid']}","{$result['lastpost_date']}"),$_SESSION["$security_session"]['subcat']) && !@in_array(array("{$result['sid']}","{$result['lastpost_date']}"),$_SESSION["$security_session"]['subcat_read']))
				{
					if($result['type'] == "1") $forum_indicator = "<img src='templates/$template/images/buttons/new.gif' alt='{$language['Forum indicator desc']}'>";
					if($result['type'] == "2") $forum_indicator = "<img src='templates/$template/images/buttons/news_new.gif' alt='{$language['Forum indicator desc2']}'>";
					if($result['type'] == "3") $forum_indicator = "<img src='templates/$template/images/buttons/admin_new.gif' alt='{$language['Forum indicator desc3']}'>";
					if($result['type'] == "4") $forum_indicator = "<img src='templates/$template/images/buttons/locked_new.gif' alt={$language['Forum indicator desc7']}>";
				}
				else
				{
					if($result['type'] == "1") $forum_indicator = "<img src='templates/$template/images/buttons/old.gif' alt='{$language['Forum indicator desc4']}'>";
					if($result['type'] == "2") $forum_indicator = "<img src='templates/$template/images/buttons/news_old.gif' alt='{$language['Forum indicator desc5']}'>";
					if($result['type'] == "3") $forum_indicator = "<img src='templates/$template/images/buttons/admin_old.gif' alt='{$language['Forum indicator desc6']}'>";
					if($result['type'] == "4") $forum_indicator = "<img src='templates/$template/images/buttons/locked_old.gif' alt='{$language['Forum indicator desc8']}'>";
				}
			}

			//-----------------------------------------------------------------
			//  Do some HTML
			//-----------------------------------------------------------------

			echo "<tr><td class='con1' align='center' width='6%'>$forum_indicator</td>";
			echo "<td class='con1'><b><a href='$PHP_SELF?n=modules/forum&a=2&d={$forum_cat['id']}&o={$result['sid']}'>{$result['name']}</a></b><br>{$result['description']}</td>";
			echo "<td class='con2' align='center'>"; if($result['type'] == "5") echo "--"; else echo $result['topics']; echo "</td>";
			echo "<td class='con2' align='center'>"; if($result['type'] == "5") echo "--"; else echo $result['replies']; echo "</td>";
			echo "<td class='con2'>";

			if(!isset($result['redirect_hits'])) $result['redirect_hits'] = "0";

			if($result['type'] == "5") echo "{$language['Forum view forum desc7']} {$result['redirect_hits']}";
			else
			{
				if(!isset($result['lastpost_date'])) echo "--";
				else
				{
					echo "<img src='templates/$template/images/buttons/date.gif' border='0' alt='{$language['Forum view forum desc6']}'> ".today_yesterday($result['lastpost_date'])."<br>{$language['Forum view forum desc6']} ";

					if(isset($result['uid'])) echo "<a href='$PHP_SELF?n=modules/users&a=1&d={$result['lastpost_id']}'>{$result['lastpost_user']}</a>";
					else echo "<a href='mailto:{$result['lastpost_email']}'>{$result['lastpost_user']}</a>";

					echo " <a href='$PHP_SELF?n=modules/forum&a=6&d={$result['lastpost_location']}'><img src='templates/$template/images/buttons/last_post.gif' border='0' alt='{$language['Forum view forum desc4']}'></a>";
				}

				echo "</td></tr>";
			}
		}

		//-----------------------------------------------------------------
		//  Show Indicators
		//-----------------------------------------------------------------

		?>
			<tr class='con4'><td colspan='5'></td></tr></table>
			<br>
			<table class="main" cellspacing="1" cellpadding="4">
			<tr><td class="con1" width="33%">
			<table width="100%" cellspacing="0" cellpadding="0">
			<tr><td width="10%" class="empty"><img src="templates/<?= $template ?>/images/buttons/new.gif" border="0" alt="<?= $language['Forum indicator desc'] ?>"></td><td width="90%" class="empty"><?= $language['Forum indicator desc'] ?></td></tr>
			</table>
			</td><td class="con1" width="33%">
			<table width="100%" cellspacing="0" cellpadding="0">
			<tr><td width="10%" class="empty"><img src="templates/<?= $template ?>/images/buttons/news_new.gif" border="0" alt="<?= $language['Forum indicator desc2'] ?>"></td><td width="90%" class="empty"><?= $language['Forum indicator desc2'] ?></td></tr>
			</table>
			</td><td class="con1" width="33%">
			<table width="100%" cellspacing="0" cellpadding="0">
			<tr><td width="10%" class="empty"><img src="templates/<?= $template ?>/images/buttons/admin_new.gif" border="0" alt="<?= $language['Forum indicator desc3'] ?>"></td><td width="90%" class="empty"><?= $language['Forum indicator desc3'] ?></td></tr>
			</table>
			</td></tr>
			<tr><td class="con1" width="33%">
			<table width="100%" cellspacing="0" cellpadding="0">
			<tr><td width="10%" class="empty"><img src="templates/<?= $template ?>/images/buttons/old.gif" border="0" alt="<?= $language['Forum indicator desc4'] ?>"></td><td width="90%" class="empty"><?= $language['Forum indicator desc4'] ?></td></tr>
			</table>
			</td><td class="con1" width="33%">
			<table width="100%" cellspacing="0" cellpadding="0">
			<tr><td width="10%" class="empty"><img src="templates/<?= $template ?>/images/buttons/news_old.gif" border="0" alt="<?= $language['Forum indicator desc5'] ?>"></td><td width="90%" class="empty"><?= $language['Forum indicator desc5'] ?></td></tr>
			</table>
			</td><td class="con1" width="33%">
			<table width="100%" cellspacing="0" cellpadding="0">
			<tr><td width="10%" class="empty"><img src="templates/<?= $template ?>/images/buttons/admin_old.gif" border="0" alt="<?= $language['Forum indicator desc6'] ?>"></td><td width="90%" class="empty"><?= $language['Forum indicator desc6'] ?></td></tr>
			</table>
			</td></tr>
			<tr><td class="con1" width="33%">
			<table width="100%" cellspacing="0" cellpadding="0">
			<tr><td width="10%" class="empty"><img src="templates/<?= $template ?>/images/buttons/locked_new.gif" border="0" alt="<?= $language['Forum indicator desc7'] ?>"></td><td width="90%" class="empty"><?= $language['Forum indicator desc7'] ?></td></tr>
			</table>
			</td><td class="con1" width="33%">
			<table width="100%" cellspacing="0" cellpadding="0">
			<tr><td width="10%" class="empty"><img src="templates/<?= $template ?>/images/buttons/locked_old.gif" border="0" alt="<?= $language['Forum indicator desc8'] ?>"></td><td width="90%" class="empty"><?= $language['Forum indicator desc8'] ?></td></tr>
			</table>
			</td><td class="con1" width="33%">
			<table width="100%" cellspacing="0" cellpadding="0">
			<tr><td width="10%" class="empty"><img src="templates/<?= $template ?>/images/buttons/redirected.gif" border="0" alt="<?= $language['Forum indicator desc9'] ?>"></td><td width="90%" class="empty"><?= $language['Forum indicator desc9'] ?></td></tr>
			</table>
			</td></tr>
			</table>
		<?

	break;

	//-----------------------------------------------------------------
	//  Show Topics
	//-----------------------------------------------------------------

	case 2:

		//-----------------------------------------------------------------
		//  Breadcrumbs + Check for existance
		//-----------------------------------------------------------------

		$forum_cat = $db->getLine("SELECT id, name FROM forum_cat",$db->escape($d));
		if(!$forum_cat) header("location: $PHP_SELF?i=error");
		$forum_subcat = $db->getLine("SELECT id, name, type, redirect_hits, redirect_location, lastpost_date FROM forum_subcat",$db->escape($o));
		if(!$forum_subcat) header("location: $PHP_SELF?i=error");

		echo "<table class='main' cellspacing='1' cellpadding='4'><tr class='con1'><td><a href='$PHP_SELF?n=modules/forum'>{$config['forum_name']}</a> &raquo; <a href='$PHP_SELF?n=modules/forum&a=1&d={$forum_cat['id']}'>{$forum_cat['name']}</a> &raquo; {$forum_subcat['name']}</td></tr></table>";

		//-----------------------------------------------------------------
		//  Start Restictions + Redirections
		//-----------------------------------------------------------------

		if($forum_subcat['type'] == "3" && isset($is_guest) || $forum_subcat['type'] == "3" && isset($is_user)){ header("location: $PHP_SELF?i=2"); exit; }

		if($forum_subcat['type'] == "5")
		{
			$redirect_plus_plus = $forum_subcat['redirect_hits'] + 1;
			$db->updateRow("forum_subcat",array('redirect_hits' => ''.$db->escape($redirect_plus_plus).''),"id = '".$db->escape($o)."'");

			header("location: http://{$forum_subcat['redirect_location']}");
		}

		//-----------------------------------------------------------------
		//  Navigation Stuff
		//-----------------------------------------------------------------

		$rows_per_page = $config['nav_topics'];
		$numrows = $db->getInfo_countRows("forum_topics","cat = '".$db->escape($d)."' AND subcat = '".$db->escape($o)."' AND mode != '1'");

		$lastpage = ceil($numrows/$rows_per_page);
		$p = (int)$p;
		
		if ($p < 1) $p = 1;
		elseif ($p > $lastpage) $p = $lastpage;

		if($numrows != FALSE) $limit = 'LIMIT ' .($p - 1) * $rows_per_page .',' .$rows_per_page;
		else $limit = NULL;

		//-----------------------------------------------------------------
		//  Load MYSQL Table
		//-----------------------------------------------------------------

		if($numrows != "0") $forum_topics = $db->getTable("SELECT t.*, ua.id AS uaid, COUNT(r.id) AS replies, ul.id AS ulid FROM forum_topics AS t LEFT JOIN users AS ua ON t.author_id=ua.id LEFT JOIN forum_replies AS r ON r.topic=t.id LEFT JOIN users AS ul ON t.lastpost_id=ul.id WHERE t.cat = '".$db->escape($d)."' AND t.subcat = '".$db->escape($o)."' AND t.mode != '1' GROUP BY t.id ORDER by mode ASC, lastpost_date DESC ".$db->escape($limit)."");
		else $forum_topics = NULL;

		//-----------------------------------------------------------------
		//  Navigation Code
		//-----------------------------------------------------------------

		if(!$forum_topics) $nav_prev = NULL;
		elseif($p == 1) $nav_prev = NULL;
		else
		{
			$prevpage = $p-1;
			$nav_prev = "<td>&nbsp;</td><td class='pagelink'><a href='$PHP_SELF?n=modules/forum&a=2&d={$forum_cat['id']}&o={$forum_subcat['id']}&p=$prevpage'>&laquo;</a></td>";
		}

		if(!$forum_topics) $nav_next = NULL;
		elseif($p == $lastpage) $nav_next = NULL;
		else
		{
			$nextpage = $p+1;
			$nav_next = "<td class='pagelink'><a href='$PHP_SELF?n=modules/forum&a=2&d={$forum_cat['id']}&o={$forum_subcat['id']}&p=$nextpage'>&raquo;</a></td>";
		}

		if($forum_topics) $nav_nr = paginate($lastpage, $p, $PHP_SELF.'?n=modules/forum&a=2&d='.$forum_cat['id'].'&o='.$forum_subcat['id'], true); else $nav_nr = NULL;

		if($lastpage == 1) $nav_pages_def = $language['Forum view topics desc11']; else $nav_pages_def = $language['Forum view topics desc12'];

		$nav = '<table border="0" cellspacing="0" cellpadding="0"><tr><td class="pagelink">'.$lastpage.' '.$nav_pages_def.'</td>'.$nav_prev.'<td>&nbsp;</td>'.$nav_nr.'<td>&nbsp;</td>'.$nav_next.'</tr></table>';

		//-----------------------------------------------------------------
		//  Make Subcat old
		//-----------------------------------------------------------------

		if(isset($is_loged_in))
		{
			if(isset($forum_topics))
				foreach($forum_topics as $idx=>$forum_topic)
					if(@in_array(array("{$forum_topic['id']}","{$forum_topic['subcat']}","{$forum_topic['lastpost_date']}"),$_SESSION["$security_session"]['topic']) && !@in_array(array("{$forum_topic['id']}","{$forum_topic['subcat']}","{$forum_topic['lastpost_date']}"),$_SESSION["$security_session"]['topic_read']))
						$forum_subcat_read = TRUE;

			if(!isset($forum_subcat_read))
			{
				if(@$_SESSION["$security_session"]['subcat'][$o] == array("{$forum_subcat['id']}","{$forum_subcat['lastpost_date']}"))
				{
					@$array_subcat_read[] = $forum_subcat['id'];
					@$array_subcat_read[] = $forum_subcat['lastpost_date'];

					$_SESSION["$security_session"]['subcat_read'][$o] = $array_subcat_read;
				}
			}
		}

		//-----------------------------------------------------------------
		//  Start Restictions
		//-----------------------------------------------------------------

		if($forum_subcat['type'] == "2" && isset($is_guest) || $forum_subcat['type'] == "2" && isset($is_user) || $forum_subcat['type'] == "2" && isset($is_moderator) || $forum_subcat['type'] == "4" && isset($is_guest) || $forum_subcat['type'] == "4" && isset($is_user)) $forum_new_topic = NULL;
		else
		{
			if($config['forum_guest_topic'] == "0" && isset($is_guest)) $forum_new_topic = NULL;
			else $forum_new_topic = "<a href='$PHP_SELF?n=modules/forum&a=4&d={$forum_cat['id']}&o={$forum_subcat['id']}'><img src='templates/$template/images/buttons/new_topic.gif' border='0' alt='{$language['Forum view topics desc10']}'></a>";
		}

		//-----------------------------------------------------------------
		//  Start display
		//-----------------------------------------------------------------

		?>
			<table><tr><td></td></tr></table>
			<table border='0' width="100%" cellspacing="0" cellpadding="0"><tr class="empty"><td align="left" width="80%"><?= $nav ?></td><td align="right" width="20%"><?= $forum_new_topic ?></td></tr></table>
			<table><tr><td></td></tr></table>
			<table class='main' cellspacing='1' cellpadding='4'><tr class='head'><td colspan='6'><img src='templates/<?= $template ?>/images/misc/nav.gif' border='0'> <?= $forum_subcat['name'] ?></td></tr>
			<tr class='bottom'><td width='40%' colspan='2'><?= $language['Forum view topics desc'] ?></td><td width='8%' align='center'><?= $language['Forum view topics desc2'] ?></td><td width='14%' align='center'><?= $language['Forum view topics desc3'] ?></td><td width='8%' align='center'><?= $language['Forum view topics desc4'] ?></td><td width='30%'><?= $language['Forum view topics desc5'] ?></td></tr>
		<?

		//-----------------------------------------------------------------
		//  Check for Announcement Topic
		//-----------------------------------------------------------------

		$forum_announcement = $db->getLine("SELECT t.*, ua.id AS uaid, COUNT(r.id) AS replies, ul.id AS ulid FROM forum_topics AS t LEFT JOIN users AS ua ON t.author_id=ua.id LEFT JOIN forum_replies AS r ON r.topic=t.id LEFT JOIN users AS ul ON t.lastpost_id=ul.id WHERE t.cat = '".$db->escape($d)."' AND t.subcat = '".$db->escape($o)."' AND t.mode = '1' GROUP BY t.id");

		//-----------------------------------------------------------------
		//  If Announcement Topic exists then show it
		//-----------------------------------------------------------------

		if($forum_announcement != FALSE)
		{
			//-----------------------------------------------------------------
			//  Do the HTML
			//-----------------------------------------------------------------

			echo "<tr><td class='con1' align='center' width='6%'><img src='templates/$template/images/buttons/announcement.gif' alt='{$language['Forum indicator desc16']}'></td>";
			echo "<td class='con1'><a href='$PHP_SELF?n=modules/forum&a=3&d={$forum_cat['id']}&o={$forum_subcat['id']}&q={$forum_announcement['id']}'>".cuttext($forum_announcement['name'],30)."</a></td>";
			echo "<td class='con2' align='center'>".($forum_announcement['replies'] - 1)."</td>";

			if(isset($forum_announcement['uaid'])) echo "<td class='con2' align='center'><a href='$PHP_SELF?n=modules/users&a=1&d={$forum_announcement['author_id']}'>{$forum_announcement['author_user']}</a></td>";
			else echo "<td class='con2' align='center'><a href='mailto:{$forum_announcement['author_email']}'>{$forum_announcement['author_user']}</a></td>";

			echo "<td class='con2' align='center'>{$forum_announcement['views']}</td>";

			//-----------------------------------------------------------------
			//  Do more HTML
			//-----------------------------------------------------------------

			if($forum_announcement['type'] == 3) echo "<td class='con2'>{$language['Forum view forum desc5']} {$forum_announcement['redirect_hits']}</td></tr>";
			else
			{
				echo "<td class='con2'><img src='templates/$template/images/buttons/date.gif' border='0' alt='{language['Forum view topics desc6']}'> ".today_yesterday($forum_announcement['lastpost_date'])."<br>{$language['Forum view topics desc7']} ";

				if(isset($forum_announcement['ulid'])) echo "<a href='$PHP_SELF?n=modules/users&a=1&d={$forum_announcement['lastpost_id']}'>{$forum_announcement['lastpost_user']}</a>";
				else echo "<a href='mailto:{$forum_announcement['lastpost_email']}'>{$forum_announcement['lastpost_user']}</a>";

				echo " <a href='$PHP_SELF?n=modules/forum&a=6&d={$forum_announcement['lastpost_location']}'><img src='templates/$template/images/buttons/last_post.gif' border='0' alt='{$language['Forum view topics desc5']}'></a></td></tr>";
			}
		}

		//-----------------------------------------------------------------
		//  Check see if topics or announcement exists, else message
		//-----------------------------------------------------------------

		if(!$forum_topics && !$forum_announcement) echo "<tr><td class='con1' colspan='6'>{$language['Forum view topics desc9']}</td></tr>";
		elseif(isset($forum_topics))
		{
			//-----------------------------------------------------------------
			//  Start the foreach if topics exist
			//-----------------------------------------------------------------

			foreach($forum_topics as $idx=>$result)
			{
				//-----------------------------------------------------------------
				//  Check see what indicator fits
				//-----------------------------------------------------------------

				if($result['mode'] == "2") $forum_indicator = "<img src='templates/$template/images/buttons/sticky.gif' alt='{$language['Forum indicator desc17']}'>";
				if($result['mode'] == "3")
				{
					if(@in_array(array("{$result['id']}","{$result['subcat']}","{$result['lastpost_date']}"),$_SESSION["$security_session"]['topic']) && !@in_array(array("{$result['id']}","{$result['subcat']}","{$result['lastpost_date']}"),$_SESSION["$security_session"]['topic_read']))
					{
						if($result['type'] == "1")
						{
							if(($result['replies'] - 1) >= $config['forum_hot']) $forum_indicator = "<img src='templates/$template/images/buttons/hot_new.gif' alt='{$language['Forum indicator desc11']}'>";
							else $forum_indicator = "<img src='templates/$template/images/buttons/new.gif' alt='{$language['Forum indicator desc10']}'>";
						}
						if($result['type'] == "2") $forum_indicator = "<img src='templates/$template/images/buttons/locked_new.gif' alt='{$language['Forum indicator desc12']}'>";
					}
					else
					{
						if($result['type'] == "1")
						{
							if(($result['replies'] - 1) >= $config['forum_hot']) $forum_indicator = "<img src='templates/$template/images/buttons/hot_old.gif' alt='{$language['Forum indicator desc14']}'>";
							else $forum_indicator = "<img src='templates/$template/images/buttons/old.gif' alt='{$language['Forum indicator desc13']}'>";
						}
						if($result['type'] == "2") $forum_indicator = "<img src='templates/$template/images/buttons/locked_old.gif' alt='{$language['Forum indicator desc15']}'>";
					}

					if($result['type'] == "3") $forum_indicator = "<img src='templates/$template/images/buttons/moved.gif' alt='{$language['Forum indicator desc18']}'>";
				}

				//-----------------------------------------------------------------
				//  Count the replies
				//-----------------------------------------------------------------

				$page = ceil($result['replies']/$config['nav_replies']);

				if($page > 1) $inner_pag = "[ ".paginate($page, 0, "$PHP_SELF?n=modules/forum&a=3&d={$forum_cat['id']}&o={$forum_subcat['id']}&q={$result['id']}")." ]";
				else $inner_pag = NULL;

				//-----------------------------------------------------------------
				//  Do some HTML
				//-----------------------------------------------------------------

				echo "<tr><td class='con1' align='center' width='6%'>$forum_indicator</td>";
				echo "<td class='con1'><a href='$PHP_SELF?n=modules/forum&a=3&d={$forum_cat['id']}&o={$forum_subcat['id']}&q={$result['id']}'>".cuttext($result['name'],30)."</a><br>$inner_pag</td>";
				if($result['type'] == 3) echo "<td class='con2' align='center'>--</td>"; else echo "<td class='con2' align='center'>".($result['replies'] - 1)."</td>";

				if(isset($result['uaid'])) echo "<td class='con2' align='center'><a href='$PHP_SELF?n=modules/users&a=1&d={$result['author_id']}'>{$result['author_user']}</a></td>";
				else  echo "<td class='con2' align='center'><a href='mailto:{$result['author_email']}'>{$result['author_user']}</a></td>";

				if($result['type'] == 3) echo "<td class='con2' align='center'>--</td>"; else echo "<td class='con2' align='center'>{$result['views']}</td>";

				if($result['type'] == 3) echo "<td class='con2'>{$language['Forum view topics desc8']} {$result['redirect_hits']}</td></tr>";
				else
				{
					echo "<td class='con2'><img src='templates/$template/images/buttons/date.gif' border='0' alt='{$language['Forum view topics desc6']}'> ".today_yesterday($result['lastpost_date'])."<br>{$language['Forum view topics desc7']} ";

					if(isset($result['ulid'])) echo "<a href='$PHP_SELF?n=modules/users&a=1&d={$result['lastpost_id']}'>{$result['lastpost_user']}</a>";
					else echo "<a href='mailto:{$result['lastpost_email']}'>{$result['lastpost_user']}</a>";

					echo " <a href='$PHP_SELF?n=modules/forum&a=6&d={$result['lastpost_location']}'><img src='templates/$template/images/buttons/last_post.gif' border='0' alt='{$language['Forum view topics desc5']}'></a></td></tr>";
				}
			}
		}

		//-----------------------------------------------------------------
		//  Show Indicators
		//-----------------------------------------------------------------

		?>
			<tr class='con4'><td colspan='6'></td></tr></table>
			<table><tr><td></td></tr></table>
			<table border='0' width="100%" cellspacing="0" cellpadding="0"><tr class="empty"><td align="left" width="80%"><?= $nav ?></td><td align="right" width="20%"><?= $forum_new_topic ?></td></tr></table>
			<table><tr><td></td></tr></table>
			<table class="main" cellspacing="1" cellpadding="4">
			<tr><td class="con1" width="33%">
			<table width="100%" cellspacing="0" cellpadding="0">
			<tr><td width="10%" class="empty"><img src="templates/<?= $template ?>/images/buttons/new.gif" border="0" alt="<?= $language['Forum indicator desc10'] ?>"></td><td width="90%" class="empty"><?= $language['Forum indicator desc10'] ?></td></tr>
			</table>
			</td><td class="con1" width="33%">
			<table width="100%" cellspacing="0" cellpadding="0">
			<tr><td width="10%" class="empty"><img src="templates/<?= $template ?>/images/buttons/hot_new.gif" border="0" alt="<?= $language['Forum indicator desc11'] ?>"></td><td width="90%" class="empty"><?= $language['Forum indicator desc11'] ?></td></tr>
			</table>
			</td><td class="con1" width="33%">
			<table width="100%" cellspacing="0" cellpadding="0">
			<tr><td width="10%" class="empty"><img src="templates/<?= $template ?>/images/buttons/locked_new.gif" border="0" alt="<?= $language['Forum indicator desc12'] ?>"></td><td width="90%" class="empty"><?= $language['Forum indicator desc12'] ?></td></tr>
			</table>
			</td></tr>
			<tr><td class="con1" width="33%">
			<table width="100%" cellspacing="0" cellpadding="0">
			<tr><td width="10%" class="empty"><img src="templates/<?= $template ?>/images/buttons/old.gif" border="0" alt="<?= $language['Forum indicator desc13'] ?>"></td><td width="90%" class="empty"><?= $language['Forum indicator desc13'] ?></td></tr>
			</table>
			</td><td class="con1" width="33%">
			<table width="100%" cellspacing="0" cellpadding="0">
			<tr><td width="10%" class="empty"><img src="templates/<?= $template ?>/images/buttons/hot_old.gif" border="0" alt="<?= $language['Forum indicator desc14'] ?>"></td><td width="90%" class="empty"><?= $language['Forum indicator desc14'] ?></td></tr>
			</table>
			</td><td class="con1" width="33%">
			<table width="100%" cellspacing="0" cellpadding="0">
			<tr><td width="10%" class="empty"><img src="templates/<?= $template ?>/images/buttons/locked_old.gif" border="0" alt="<?= $language['Forum indicator desc15'] ?>"></td><td width="90%" class="empty"><?= $language['Forum indicator desc15'] ?></td></tr>
			</table>
			</td></tr>
			<tr><td class="con1" width="33%">
			<table width="100%" cellspacing="0" cellpadding="0">
			<tr><td width="10%" class="empty"><img src="templates/<?= $template ?>/images/buttons/announcement.gif" border="0" alt="<?= $language['Forum indicator desc16'] ?>"></td><td width="90%" class="empty"><?= $language['Forum indicator desc16'] ?></td></tr>
			</table>
			</td><td class="con1" width="33%">
			<table width="100%" cellspacing="0" cellpadding="0">
			<tr><td width="10%" class="empty"><img src="templates/<?= $template ?>/images/buttons/sticky.gif" border="0" alt="<?= $language['Forum indicator desc17'] ?>"></td><td width="90%" class="empty"><?= $language['Forum indicator desc17'] ?></td></tr>
			</table>
			</td><td class="con1" width="33%">
			<table width="100%" cellspacing="0" cellpadding="0">
			<tr><td width="10%" class="empty"><img src="templates/<?= $template ?>/images/buttons/moved.gif" border="0" alt="<?= $language['Forum indicator desc18'] ?>"></td><td width="90%" class="empty"><?= $language['Forum indicator desc18'] ?></td></tr>
			</table>
			</td></tr>
			</table>
		<?

	break;

	//-----------------------------------------------------------------
	//  Show Replies
	//-----------------------------------------------------------------

	case 3:

		//-----------------------------------------------------------------
		//  Breadcrumbs + Check for existance
		//-----------------------------------------------------------------

		$forum_cat = $db->getLine("SELECT * FROM forum_cat",$db->escape($d));
		if(!$forum_cat) header("location: $PHP_SELF?i=error");
		$forum_subcat = $db->getLine("SELECT * FROM forum_subcat",$db->escape($o));
		if(!$forum_subcat) header("location: $PHP_SELF?i=error");
		$forum_topic = $db->getLine("SELECT * FROM forum_topics",$db->escape($q));
		if(!$forum_topic) header("location: $PHP_SELF?i=error");

		echo "<table class='main' cellspacing='1' cellpadding='4'><tr class='con1'><td><a href='$PHP_SELF?n=modules/forum'>{$config['forum_name']}</a> &raquo; <a href='$PHP_SELF?n=modules/forum&a=1&d={$forum_cat['id']}'>{$forum_cat['name']}</a> &raquo; <a href='$PHP_SELF?n=modules/forum&a=2&d={$forum_cat['id']}&o={$forum_subcat['id']}'>{$forum_subcat['name']}</a> &raquo; {$forum_topic['name']}</td></tr></table>";

		//-----------------------------------------------------------------
		//  Start Redirections
		//-----------------------------------------------------------------

		if($forum_topic['type'] == "3")
		{
			$redirect_plus_plus = $forum_topic['redirect_hits'] + 1;
			$db->updateRow("forum_topics",array('redirect_hits' => ''.$db->escape($redirect_plus_plus).''),"id = '".$db->escape($q)."'");

			header("location: $PHP_SELF?n=modules/forum&a=3&d={$forum_topic['redirect_cat']}&o={$forum_topic['redirect_subcat']}&q={$forum_topic['redirect_topic']}");
		}

		//-----------------------------------------------------------------
		//  Start Restictions
		//-----------------------------------------------------------------

		if($forum_subcat['type'] == "3" && isset($is_guest) || $forum_subcat['type'] == "3" && isset($is_user)){ header("location: $PHP_SELF?i=2"); exit; }

		//-----------------------------------------------------------------
		//  Count Views
		//-----------------------------------------------------------------

		$view_plus_plus = $forum_topic['views'] + 1;
		$db->updateRow("forum_topics",array('views' => ''.$db->escape($view_plus_plus).''),"id = '".$db->escape($q)."'");

		//-----------------------------------------------------------------
		//  Navigation Stuff
		//-----------------------------------------------------------------

		$rows_per_page = $config['nav_replies'];
		$numrows = $db->getInfo_countRows("forum_replies","cat = '".$db->escape($d)."' AND subcat = '".$db->escape($o)."' AND topic = '".$db->escape($q)."'");

		$lastpage = ceil($numrows/$rows_per_page);
		$p = (int)$p;

		if ($p < 1) $p = 1;
		elseif ($p > $lastpage) $p = $lastpage;

		if($numrows != FALSE) $limit = 'LIMIT ' .($p - 1) * $rows_per_page .',' .$rows_per_page;
		else $limit = NULL;

		//-----------------------------------------------------------------
		//  Load MYSQL Table
		//-----------------------------------------------------------------

		$results = $db->getTable("SELECT r.id, r.date, r.user_reg, r.user_id, r.user_user, r.user_email, r.message, r.ip, u.id AS uid, u.authorization, u.email, u.url, u.flag, u.location, u.registered, u.forumposts, u.signature, u.msn, u.yahoo, u.icq, u.aim, u.skype, u.avatar, u.hideemail, u.status, o.user_id AS oid FROM forum_replies AS r LEFT JOIN users AS u ON u.id=r.user_id LEFT JOIN online AS o ON (o.user_id=u.id AND o.type='live') WHERE r.cat = '".$db->escape($d)."' AND r.subcat = '".$db->escape($o)."' AND r.topic = '".$db->escape($q)."' ORDER by id ASC ".$db->escape($limit)."");

		//-----------------------------------------------------------------
		//  Navigation Code
		//-----------------------------------------------------------------

		if(!$results) $nav_prev = NULL;
		elseif($p == 1) $nav_prev = NULL;
		else
		{
			$prevpage = $p-1;
			$nav_prev = "<td>&nbsp;</td><td class='pagelink'><a href='$PHP_SELF?n=modules/forum&a=3&d={$forum_cat['id']}&o={$forum_subcat['id']}&q={$forum_topic['id']}&p=$prevpage'>&laquo;</a></td>";
		}

		if(!$results) $nav_next = NULL;
		elseif($p == $lastpage) $nav_next = NULL;
		else
		{
			$nextpage = $p+1;
			$nav_next = "<td class='pagelink'><a href='$PHP_SELF?n=modules/forum&a=3&d={$forum_cat['id']}&o={$forum_subcat['id']}&q={$forum_topic['id']}&p=$nextpage'>&raquo;</a></td>";
		}

		if($results) $nav_nr = paginate($lastpage, $p, $PHP_SELF.'?n=modules/forum&a=3&d='.$forum_cat['id'].'&o='.$forum_subcat['id'].'&q='.$forum_topic['id'], true); else $nav_nr = NULL;

		if($lastpage == 1) $nav_pages_def = $language['Forum view replies desc26']; else $nav_pages_def = $language['Forum view replies desc27'];

		$nav = '<table border="0" cellspacing="0" cellpadding="0"><tr><td class="pagelink">'.$lastpage.' '.$nav_pages_def.'</td>'.$nav_prev.'<td>&nbsp;</td>'.$nav_nr.'<td>&nbsp;</td>'.$nav_next.'</tr></table>';

		//-----------------------------------------------------------------
		//  Make Topic old
		//-----------------------------------------------------------------

		if(isset($is_loged_in))
		{
			if(@$_SESSION["$security_session"]['topic'][$q] == array("{$forum_topic['id']}","{$forum_topic['subcat']}","{$forum_topic['lastpost_date']}"))
			{
				@$array_topic_read[] = $forum_topic['id'];
				@$array_topic_read[] = $forum_topic['subcat'];
				@$array_topic_read[] = $forum_topic['lastpost_date'];

				$_SESSION["$security_session"]['topic_read'][$q] = $array_topic_read;

				$tmp['forum_lastvisit'] = $forum_topic['lastpost_date'] + 1;
				$db->updateRow("users",$tmp,"id=".$db->escape($_SESSION["$security_session"]['id'])."");
			}
		}

		//-----------------------------------------------------------------
		//  Make Subcat old
		//-----------------------------------------------------------------

		if(isset($is_loged_in))
		{
			$forum_subcat_topics = $db->getTable("SELECT id, subcat, lastpost_date FROM forum_topics WHERE cat = '".$db->escape($d)."' AND subcat = '".$db->escape($o)."'");

			foreach($forum_subcat_topics as $idx=>$forum_subcat_topic)
				if(@in_array(array("{$forum_subcat_topic['id']}","{$forum_subcat_topic['subcat']}","{$forum_subcat_topic['lastpost_date']}"),$_SESSION["$security_session"]['topic']) && !@in_array(array("{$forum_subcat_topic['id']}","{$forum_subcat_topic['subcat']}","{$forum_subcat_topic['lastpost_date']}"),$_SESSION["$security_session"]['topic_read']))
					$forum_subcat_read = TRUE;

			if(!isset($forum_subcat_read))
			{
				if(@$_SESSION["$security_session"]['subcat'][$o] == array("{$forum_subcat['id']}","{$forum_subcat['lastpost_date']}"))
				{
					@$array_subcat_read[] = $forum_subcat['id'];
					@$array_subcat_read[] = $forum_subcat['lastpost_date'];

					$_SESSION["$security_session"]['subcat_read'][$o] = $array_subcat_read;
				}
			}
		}

		//-----------------------------------------------------------------
		//  Start Restictions
		//-----------------------------------------------------------------

		if($forum_subcat['type'] == "2" && isset($is_guest) || $forum_subcat['type'] == "2" && isset($is_user) || $forum_subcat['type'] == "2" && isset($is_moderator) || $forum_subcat['type'] == "4" && isset($is_guest) || $forum_subcat['type'] == "4" && isset($is_user)) $forum_new_topic = NULL;
		else
		{
			if($config['forum_guest_topic'] == "0" && isset($is_guest)) $forum_new_topic = NULL;
			else $forum_new_topic = "<td align='right' width='15%'><a href='$PHP_SELF?n=modules/forum&a=4&d={$forum_cat['id']}&o={$forum_subcat['id']}'><img src='templates/$template/images/buttons/new_topic.gif' border='0' alt='{$language['Forum view replies desc25']}'></a></td>";
		}

		if($forum_topic['mode'] == "1" && isset($is_guest) || $forum_topic['mode'] == "1" && isset($is_user) || $forum_topic['mode'] == "1" && isset($is_moderator) || $forum_subcat['type'] == "4" && isset($is_guest) || $forum_subcat['type'] == "4" && isset($is_user)) $forum_add_reply = NULL;
		else
		{
			if($forum_topic['type'] == "2" && isset($is_guest) || $forum_topic['type'] == "2" && isset($is_user) || $forum_topic['mode'] == "2" && isset($is_guest) || $forum_topic['mode'] == "2" && isset($is_user)) $forum_add_reply = NULL;
			else
			{
				if($config['forum_guest_reply'] == "0" && isset($is_guest)) $forum_add_reply = NULL;
				else $forum_add_reply = "<td align='right' width='15%'><a href='$PHP_SELF?n=modules/forum&a=5&d={$forum_cat['id']}&o={$forum_subcat['id']}&q={$forum_topic['id']}&p=$lastpage'><img src='templates/$template/images/buttons/add_reply.gif' border='0' alt='{$language['Forum view replies desc24']}'></a></td>";
			}
		}

		//-----------------------------------------------------------------
		//  Start display
		//-----------------------------------------------------------------

		?>
			<table><tr><td></td></tr></table>
			<table border='0' width="100%" cellspacing="0" cellpadding="0"><tr class="empty"><td align="left" width="70%"><?= $nav ?></td><?= $forum_add_reply ?><?= $forum_new_topic ?><td align='right' width='15%'><a href="<?=$PHP_SELF?>?n=modules/rss&a=1&l=<?=$forum_topic['id']?>"><img src="images/icons_admin/rss.gif" border='0' alt="RSS Feed for Forum Topic" /></a></td></tr></table>
			<table><tr><td></td></tr></table>
			<table class='main' cellspacing='1' cellpadding='4'><tr class='head'><td colspan='3'><img src='templates/<?= $template ?>/images/misc/nav.gif' border='0'> <?= $forum_topic['name'] ?></td></tr>
		<?

		//-----------------------------------------------------------------
		//  Get Ranks and first min post
		//-----------------------------------------------------------------

		$ranks_db = $db->getTable("SELECT * FROM forum_ranks");
		$first_rank = $db->getLine("SELECT * FROM forum_ranks ORDER BY posts LIMIT 1");

		//-----------------------------------------------------------------
		//  Start foreach
		//-----------------------------------------------------------------

		foreach($results as $idx=>$result)
		{
			//-----------------------------------------------------------------
			//  Get correct post count
			//-----------------------------------------------------------------

			$correct_post_count = $idx + (($p - 1) * $rows_per_page + 1);

			//-----------------------------------------------------------------
			//  Choose either member or guest
			//-----------------------------------------------------------------

			if(isset($result['uid']))
			{
				//-----------------------------------------------------------------
				//  Check see if user is online
				//-----------------------------------------------------------------

				if(isset($result['oid'])) $result['online'] = "<td width='21%'><img src='templates/$template/images/buttons/on.gif' alt='{$language['Forum view replies desc8']}'></td>";
				else $result['online'] = "<td width='24%'><img src='templates/$template/images/buttons/off.gif' alt='{$language['Forum view replies desc9']}'></td>";

				//-----------------------------------------------------------------
				//  Check and get the extra buttons from user's profile
				//-----------------------------------------------------------------

				if($result['hideemail'] != "1") $result_email = "<td width='1%'><a href='mailto:{$result['email']}'><img src='templates/$template/images/buttons/email.gif' border='0' alt='{$language['Forum view replies desc12']}'></a></td><td width='3'></td>"; else $result_email = NULL;
				if(isset($result['url'])) $result_website = "<td width='1%'><a href='http://{$result['url']}' target='_blank'><img src='templates/$template/images/buttons/www.gif' border='0' alt='{$language['Forum view replies desc13']}'></a></td><td width='3'></td>"; else $result_website = NULL;
				if(isset($result['msn'])) $result_msn = "<td width='1%'><a href='$PHP_SELF?n=modules/users&a=1&d={$result['uid']}'><img src='templates/$template/images/buttons/msn.gif' border='0' alt='{$language['Forum view replies desc14']}'></a></td><td width='3'></td>"; else $result_msn = NULL;
				if(isset($result['yahoo'])) $result_yahoo = "<td width='1%'><a href='$PHP_SELF?n=modules/users&a=1&d={$result['uid']}'><img src='templates/$template/images/buttons/yahoo.gif' border='0' alt='{$language['Forum view replies desc15']}'></a></td><td width='3'></td>"; else $result_yahoo = NULL;
				if(isset($result['icq'])) $result_icq = "<td width='1%'><a href='$PHP_SELF?n=modules/users&a=1&d={$result['uid']}'><img src='templates/$template/images/buttons/icq.gif' border='0' alt='{$language['Forum view replies desc16']}'></a></td><td width='3'></td>"; else $result_icq = NULL;
				if(isset($result['aim'])) $result_aim = "<td width='1%'><a href='$PHP_SELF?n=modules/users&a=1&d={$result['uid']}'><img src='templates/$template/images/buttons/aim.gif' border='0' alt='{$language['Forum view replies desc17']}'></a></td><td width='3'></td>"; else $result_aim = NULL;
				if(isset($result['skype'])) $result_skype = "<td width='1%'><a href='$PHP_SELF?n=modules/users&a=1&d={$result['uid']}'><img src='templates/$template/images/buttons/skype.gif' border='0' alt='{$language['Forum view replies desc18']}'></a></td><td width='3'></td>"; else $result_skype = NULL;
				if(isset($result['signature'])) $user_signature = "<br><br><br>{$language['Forum view replies desc7']}<br>".parse_message($result['signature']); else $user_signature = NULL;

				//-----------------------------------------------------------------
				//  Get the specific rank
				//-----------------------------------------------------------------

				if($ranks_db != FALSE)
				{
					if($result['authorization'] != 1) $rank = find_authorization($result['authorization']);
					else
					{
						if($result['status'] == "banned") $rank = find_status($result['status']);
						else
						{
							if ($result['forumposts'] < $first_rank['posts']) $rank = find_authorization($result['authorization']);

							foreach($ranks_db as $idx=>$cur_rank) if ($result['forumposts'] >= $cur_rank['posts']) $rank = $cur_rank['name'];
						}
					}
				}
				else $rank = find_authorization($result['authorization']);

				//-----------------------------------------------------------------
				//  Do the HTML
				//-----------------------------------------------------------------

				echo "<tr class='bottom'><td width='25%'><a name='{$result['id']}'></a><span style='font-size: 12px; font-weight: bold;'><a href='$PHP_SELF?n=modules/users&a=1&d={$result['user_id']}'>{$result['user_user']}</a></span></td><td width='75%' colspan='2' align='center'><table width='100%' cellspacing='0' cellpadding='0'><tr class='empty'><td width='80%' align='left'><b><font color='#848484'><img src='templates/$template/images/buttons/date.gif' border='0' alt='{$language['Forum view replies desc6']}'> ".today_yesterday($result['date'])."</font></b></td><td width='20%' align='right'><span class='inv'>#$correct_post_count</span></td></tr></table></td></tr>";
				echo "<tr><td class='con1' valign='top'><table border='0' cellspacing='0' cellpadding='0'><tr class='empty'><td width='79' height='80' class='avatar'>".find_avatar($result['avatar'])."</td></tr></table>$rank<br><br>{$language['Forum view replies desc']} ".date("d-M y",$result['registered'])."<br>"; if(find_flag($result['flag'],$result['location'], true, true)) echo "{$language['Forum view replies desc1']} ".find_flag($result['flag'],$result['location'], true, true)."<br>"; echo "{$language['Forum view replies desc2']} {$result['forumposts']}";

				if(isset($is_administrator) || isset($is_webmaster)) echo "<br><br>{$language['Forum view replies desc3']} <a href='http://www.dnsstuff.com/tools/whois.ch?ip={$result['ip']}' target='_blank' title='{$language['Forum view replies desc4']}'>{$result['ip']}</a></td>";
				else echo "</td>";

				echo "<td valign='top' class='con1'>".parse_message($result['message'])."$user_signature</td>";

				//-----------------------------------------------------------------
				//  Do the Admin buttons
				//-----------------------------------------------------------------

				if(isset($is_administrator) && $result['authorization'] != "4" || isset($is_webmaster) || isset($is_moderator) && $result['authorization'] < "3" ||  $result['user_id'] == @$_SESSION["$security_session"]['id'])
				{
					if($forum_topic['mode'] == "1" && isset($is_guest) || $forum_topic['mode'] == "1" && isset($is_user) || $forum_topic['mode'] == "1" && isset($is_moderator))
						echo "<td width='1%' class='con2' valign='top'></td>";
					else
					{
						echo "<td width='1%' class='con2' valign='top'>";

						if($correct_post_count == 1)
						{
							if(isset($is_administrator) || isset($is_webmaster) || isset($is_moderator)) if($forum_topic['mode'] != "1") echo "<a href='$PHP_SELF?n=modules/forum&a=7&d={$forum_cat['id']}&o={$forum_subcat['id']}&q={$forum_topic['id']}'><img src='templates/$template/images/buttons/moderate.gif' border='0' alt='{$language['Forum view replies desc23']}'></a><table><tr><td></td></tr></table>";
							echo "<a href='$PHP_SELF?n=modules/forum&a=10&d={$forum_cat['id']}&o={$forum_subcat['id']}&q={$forum_topic['id']}'><img src='templates/$template/images/buttons/delete2.gif' border='0' alt='{$language['Forum view replies desc21']}'></a>";
						}
						else echo "<a href='$PHP_SELF?n=modules/forum&a=9&d={$forum_cat['id']}&o={$forum_subcat['id']}&q={$forum_topic['id']}&k={$result['id']}&p=$lastpage'><img src='templates/$template/images/buttons/delete2.gif' border='0' alt='{$language['Forum view replies desc22']}'></a>";

						echo "<table><tr><td></td></tr></table><a href='$PHP_SELF?n=modules/forum&a=8&d={$forum_cat['id']}&o={$forum_subcat['id']}&q={$forum_topic['id']}&k={$result['id']}&p=$lastpage'><img src='templates/$template/images/buttons/edit2.gif' border='0' alt='{$language['Forum view replies desc20']}'></a></td>";				
					}
				}
				else echo "<td width='1%' class='con2' valign='top'></td>";

				//-----------------------------------------------------------------
				//  Do more HTML
				//-----------------------------------------------------------------

				echo "</tr><tr class='con2'><td><table cellspacing='0' cellpadding='0' width='100%'><tr class='empty'>{$result['online']}<td><a href='$PHP_SELF?n=modules/users&a=1&d={$result['uid']}'><img src='templates/$template/images/buttons/profile.gif' border='0' alt='{$language['Forum view replies desc10']}'></a></td><td align='right'><a href='$PHP_SELF?n=modules/pm&a=4&d={$result['uid']}'><img src='templates/$template/images/buttons/pm.gif' border='0' alt='{$language['Forum view replies desc11']}'></a></td></tr></table></td><td colspan='2'><table cellspacing='0' cellpadding='0' width='100%'><tr class='empty'>$result_email$result_website$result_msn$result_yahoo$result_icq$result_aim$result_skype<td align='right'><a href='$PHP_SELF?n=modules/forum&a=5&d={$forum_cat['id']}&o={$forum_subcat['id']}&q={$forum_topic['id']}&p=$lastpage&k={$result['id']}'><img src='templates/$template/images/buttons/quote.gif' border='0' alt='{$language['Forum view replies desc19']}'></a></td></tr></table></td></tr>";
				echo "<tr class='con4'><td colspan='3'></td></tr>";
			}
			else
			{
				//-----------------------------------------------------------------
				//  Do the HTML
				//-----------------------------------------------------------------

				echo "<tr class='bottom'><td width='25%'><a name='{$result['id']}'></a><span style='font-size: 12px; font-weight: bold;'><a href='mailto:{$result['user_email']}'>{$result['user_user']}</a></span></td><td width='75%' colspan='2' align='center'><table width='100%' cellspacing='0' cellpadding='0'><tr class='empty'><td width='80%' align='left'><b><font color='#848484'><img src='templates/$template/images/buttons/date.gif' border='0' alt='{$language['Forum view replies desc6']}'> ".today_yesterday($result['date'])."</font></b></td><td width='20%' align='right'><span class='inv'>#$correct_post_count</span></td></tr></table></td></tr>";
				echo "<tr><td class='con1' valign='top'>{$language['Forum view replies desc5']}";

				if(isset($is_administrator) || isset($is_webmaster)) echo "<br><br>{$language['Forum view replies desc3']} <a href='http://www.dnsstuff.com/tools/whois.ch?ip={$result['ip']}' target='_blank' title='{$language['Forum view replies desc4']}'>{$result['ip']}</a></td>";
				else echo "</td>";

				echo "<td valign='top' class='con1'>".parse_message($result['message'])."</td>";

				//-----------------------------------------------------------------
				//  Do the Admin buttons
				//-----------------------------------------------------------------

				if(isset($is_administrator) || isset($is_webmaster) || isset($is_moderator) || $result['ip'] == find_ip() && !isset($is_user))
				{
					echo "<td width='1%' class='con2' valign='top'>";

					if($correct_post_count == 1)
					{
						if(isset($is_administrator) || isset($is_webmaster) || isset($is_moderator)) echo "<a href='$PHP_SELF?n=modules/forum&a=7&d={$forum_cat['id']}&o={$forum_subcat['id']}&q={$forum_topic['id']}'><img src='templates/$template/images/buttons/moderate.gif' border='0' alt='{$language['Forum view replies desc23']}'></a><table><tr><td></td></tr></table>";
						echo "<a href='$PHP_SELF?n=modules/forum&a=10&d={$forum_cat['id']}&o={$forum_subcat['id']}&q={$forum_topic['id']}'><img src='templates/$template/images/buttons/delete2.gif' border='0' alt='{$language['Forum view replies desc21']}'></a>";
					}
					else echo "<a href='$PHP_SELF?n=modules/forum&a=9&d={$forum_cat['id']}&o={$forum_subcat['id']}&q={$forum_topic['id']}&k={$result['id']}&p=$lastpage'><img src='templates/$template/images/buttons/delete2.gif' border='0' alt='{$language['Forum view replies desc22']}'></a>";

					echo "<table><tr><td></td></tr></table><a href='$PHP_SELF?n=modules/forum&a=8&d={$forum_cat['id']}&o={$forum_subcat['id']}&q={$forum_topic['id']}&k={$result['id']}&p=$lastpage'><img src='templates/$template/images/buttons/edit2.gif' border='0' alt='{$language['Forum view replies desc20']}'></a></td>";
				}
				else echo "<td width='1%' class='con2' valign='top'></td>";

				//-----------------------------------------------------------------
				//  Do more HTML
				//-----------------------------------------------------------------

				echo "</tr><tr class='con2'><td></td><td colspan='2'><table cellspacing='0' cellpadding='0' width='100%'><tr class='empty'><td><a href='mailto:{$result['user_email']}'><img src='templates/$template/images/buttons/email.gif' border='0' alt='{$language['Forum view replies desc12']}'></a></td><td align='right'><a href='$PHP_SELF?n=modules/forum&a=5&d={$forum_cat['id']}&o={$forum_subcat['id']}&q={$forum_topic['id']}&p=$lastpage&k={$result['id']}'><img src='templates/$template/images/buttons/quote.gif' border='0' alt='{$language['Forum view replies desc19']}'></a></td></tr></table></td></tr>";
				echo "<tr class='con4'><td colspan='3'></td></tr>";
			}
		}

		?>
			</table>
			<table><tr><td></td></tr></table>
			<table border='0' width="100%" cellspacing="0" cellpadding="0"><tr class="empty">
			<td align="left" width="70%"><?= $nav ?></td><?= $forum_add_reply ?><?= $forum_new_topic ?></tr></table>
			<table><tr><td></td></tr></table>
		<?

	break;

	//-----------------------------------------------------------------
	//  Add Topic
	//-----------------------------------------------------------------

	case 4:

		//-----------------------------------------------------------------
		//  Start secondary switch
		//-----------------------------------------------------------------

		switch($c)
		{
			//-----------------------------------------------------------------
			//  Add the topic
			//-----------------------------------------------------------------

			case 1:

				//-----------------------------------------------------------------
				//  Start Checks
				//-----------------------------------------------------------------

				if(!isset($is_loged_in))
				{
					$id = NULL;
					if(!$username){ header("location: $PHP_SELF?n=modules/forum&a=4&d=$d&o=$o&i=96"); exit; }
					$username = jcm_htmlspecialchars(do_censor($username));
					if(!$email){ header("location: $PHP_SELF?n=modules/forum&a=4&d=$d&o=$o&i=97"); exit; }
					if(!eregi('^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.([a-zA-Z]{2,4})$', $email)){ header("location: $PHP_SELF?n=modules/forum&a=4&d=$d&o=$o&i=98"); exit; }
					$topic_type = 3;
					$author_reg = 1;
				}
				else
				{
					$user = $db->getLine("SELECT id, username, email, forumposts FROM users WHERE id = '".$db->escape($_SESSION["$security_session"]['id'])."' AND username = '".$db->escape($_SESSION["$security_session"]['username'])."'");
					$id = $user['id'];
					$username = $user['username'];
					$email = $user['email'];
					if(isset($is_moderator) || isset($is_administrator) || isset($is_webmaster)) $topic_type = $topic_type;
					else $topic_type = 3;
					$author_reg = 2;
				}

				if(!$topic_name){ header("location: $PHP_SELF?n=modules/forum&a=4&d=$d&o=$o&i=99"); exit; }
				if(!$sendmessage){ header("location: $PHP_SELF?n=modules/forum&a=4&d=$d&o=$o&i=100"); exit; }

				//-----------------------------------------------------------------
				//  BBcode check
				//-----------------------------------------------------------------

				for ($i = 1; $i <= 5; $i++) if(check_tag_order($sendmessage) == $i) { header("location: $PHP_SELF?n=modules/forum&a=4&d=$d&o=$o&i=9$i"); exit; }

				//-----------------------------------------------------------------
				//  Take care of some things
				//-----------------------------------------------------------------

				$topic_name = trim(jcm_stripslashes(do_censor(jcm_htmlspecialchars($topic_name))));
				$sendmessage = jcm_linebreaks(trim(jcm_stripslashes($sendmessage)));
				if($overflow = check_tag_order($sendmessage)) $sendmessage = substr($sendmessage, 0, $overflow[0]).substr($sendmessage, $overflow[1], (strlen($sendmessage) - $overflow[0]));

				//-----------------------------------------------------------------
				//  Write in database
				//-----------------------------------------------------------------

				$db->insertRow("forum_topics",array('cat' => ''.$db->escape($d).'','subcat' => ''.$db->escape($o).'','date' => ''.time().'','name' => ''.$db->escape($topic_name).'','author_reg' => ''.$db->escape($author_reg).'','author_id' => ''.$db->escape($id).'','author_user' => ''.$db->escape($username).'','author_email' => ''.$db->escape($email).'','mode' => ''.$db->escape($topic_type).'','ip' => ''.find_ip().''));
				$check_topic = $db->getLine("SELECT * FROM forum_topics WHERE cat = '".$db->escape($d)."' AND subcat = '".$db->escape($o)."' ORDER BY id DESC");
				$db->insertRow("forum_replies",array('cat' => ''.$db->escape($d).'','subcat' => ''.$db->escape($o).'','topic' => ''.$db->escape($check_topic['id']).'','date' => ''.time().'','user_reg' => ''.$db->escape($author_reg).'','user_id' => ''.$db->escape($id).'','user_user' => ''.$db->escape($username).'','user_email' => ''.$db->escape($email).'','message' => ''.$sendmessage.'','ip' => ''.find_ip().''));
				$check_reply = $db->getLine("SELECT * FROM forum_replies ORDER BY id DESC");
				$db->updateRow("forum_subcat",array('lastpost_location' => ''.$db->escape($check_reply['id']).'','lastpost_date' => ''.time().'','lastpost_reg' => ''.$db->escape($author_reg).'','lastpost_id' => ''.$db->escape($id).'','lastpost_user' => ''.$db->escape($username).'','lastpost_email' => ''.$db->escape($email).''),"id = '".$db->escape($o)."'");
				$db->updateRow("forum_topics",array('lastpost_location' => ''.$db->escape($check_reply['id']).'','lastpost_date' => ''.time().'','lastpost_reg' => ''.$db->escape($author_reg).'','lastpost_id' => ''.$db->escape($id).'','lastpost_user' => ''.$db->escape($username).'','lastpost_email' => ''.$db->escape($email).''),"id = '".$db->escape($check_topic['id'])."'");
				$check_topics = $db->getInfo_countRows("forum_topics","cat = '".$db->escape($d)."' AND subcat = '".$db->escape($o)."' AND type != '3'");
				$check_replies = $db->getInfo_countRows("forum_replies","cat = '".$db->escape($d)."' AND subcat = '".$db->escape($o)."'");
				$db->updateRow("forum_subcat",array('topics' => ''.$db->escape($check_topics).'', 'replies' => ''.$db->escape($check_replies).''),"id = '".$db->escape($o)."'");

				if($author_reg == "2")
				{
					$plus_forumposts = $user['forumposts'] + 1;
					$db->updateRow("users",array('forumposts' => ''.$db->escape($plus_forumposts).''),"id = '".$db->escape($user['id'])."'");
				}

				//-----------------------------------------------------------------
				//  Redirect when finished
				//-----------------------------------------------------------------

				header("location: $PHP_SELF?n=modules/forum&a=3&d=$d&o=$o&q={$check_topic['id']}&i=101");

			break;

			//-----------------------------------------------------------------
			//  View topic Form
			//-----------------------------------------------------------------

			default:

				//-----------------------------------------------------------------
				//  If user is guest and restriction is turned on
				//-----------------------------------------------------------------

				if($config['forum_guest_topic'] == "0" && isset($is_guest))
				{ header("location: $PHP_SELF?i=2"); exit; }

				//-----------------------------------------------------------------
				//  Breadcrumbs
				//-----------------------------------------------------------------

				$forum_cat = $db->getLine("SELECT * FROM forum_cat",$db->escape($d));
				if(!$forum_cat) header("location: $PHP_SELF?i=error");
				$forum_subcat = $db->getLine("SELECT * FROM forum_subcat",$db->escape($o));
				if(!$forum_subcat) header("location: $PHP_SELF?i=error");

				echo "<table class='main' cellspacing='1' cellpadding='4'><tr class='con1'><td><a href='$PHP_SELF?n=modules/forum'>{$config['forum_name']}</a> &raquo; <a href='$PHP_SELF?n=modules/forum&a=1&d={$forum_cat['id']}'>{$forum_cat['name']}</a> &raquo; <a href='$PHP_SELF?n=modules/forum&a=2&d={$forum_cat['id']}&o={$forum_subcat['id']}'>{$forum_subcat['name']}</a></td></tr></table>";

				//-----------------------------------------------------------------
				//  Check for Announcement
				//-----------------------------------------------------------------

				$forum_topics = $db->getTable("SELECT * FROM forum_topics WHERE subcat = '".$db->escape($o)."'");

				$found = FALSE;
				foreach($forum_topics as $forum_topic) { if($forum_topic['mode'] == 1) $found = TRUE; }

				//-----------------------------------------------------------------
				//  Start Restrictions
				//-----------------------------------------------------------------

				if($forum_subcat['type'] == "2" && isset($is_guest) || $forum_subcat['type'] == "2" && isset($is_user) || $forum_subcat['type'] == "2" && isset($is_moderator)){ header("location: $PHP_SELF?i=2"); exit; }
				if($forum_subcat['type'] == "3" && isset($is_guest) || $forum_subcat['type'] == "3" && isset($is_user)){ header("location: $PHP_SELF?i=2"); exit; }
				if($forum_subcat['type'] == "4" && isset($is_guest) || $forum_subcat['type'] == "4" && isset($is_user)){ header("location: $PHP_SELF?i=2"); exit; }
				if($forum_subcat['type'] == "5") header("location: http://{$forum_subcat['redirect_location']}");

				//-----------------------------------------------------------------
				//  Start Display Form
				//-----------------------------------------------------------------

				?>
					<br>
					<form action="<?= $PHP_SELF ?>?n=modules/forum" method="post" name="textarea">
					<table class="main" cellspacing="1" cellpadding="4">
					<col width="25%"><col width="75%">
					<tr class="head"><td colspan="2"><?= $language['Forum add topic desc'] ?> [<?= $forum_subcat['name'] ?>]</td></tr>
					<? if(!isset($is_loged_in)) { ?>
					<tr class="con1"><td><?= $language['Forum add topic desc2'] ?></td><td><input style="width:40%" maxlength="35" class="box" name="username"></td></tr>
					<tr class="con1"><td><?= $language['Forum add topic desc3'] ?></td><td><input style="width:40%" maxlength="35" class="box" name="email"></td></tr>
					<? } ?>
					<tr class="con1"><td><?= $language['Forum add topic desc4'] ?></td><td><input style="width:40%" maxlength="50" class="box" name="topic_name"></td></tr>
					<? if(isset($is_moderator) || isset($is_administrator) || isset($is_webmaster)) { ?>
					<tr class="con1"><td><?= $language['Forum add topic desc5'] ?></td><td><select style="width:40%" class="box" name="topic_type"><option value="3"><?= $language['Forum add topic desc6'] ?></option><? if(!isset($is_moderator)) { if($found == FALSE){ ?><option value="1"><?= $language['Forum add topic desc7'] ?></option><? }} ?><option value="2"><?= $language['Forum add topic desc8'] ?></option></td></tr>
					<? } if($config['emoticons'] == 1) { ?>
					<tr class="con1">
					<td><?= $language['Forum add topic desc9'] ?></td>
					<td><?= insertSmilies(20) ?></td>
					</tr>
					<? } if($config['bbcode'] == 1) { ?>
					<tr class="con1">
					<td><?= $language['Forum add topic desc10'] ?></td>
					<td><?= insertBBcode(20) ?></td>
					</tr>
					<? } ?>
					<tr class="con1">
					<td valign="top"><?= $language['Forum add topic desc11'] ?><?= dis_bb_em(true) ?></td>
					<td><textarea style="width:100%; height:205px;" class="box" name="sendmessage"></textarea></td>
					</tr>
					<tr class="con1">
					<td><?= $language['Forum add topic desc12'] ?></td>
					<td><input type="submit" style="width:120" class="box" value="<?= $language['Forum add topic desc13'] ?>"></td>
					</tr>
					</table>
					<input type="hidden" name="a" value="4">
					<input type="hidden" name="c" value="1">
					<input type="hidden" name="d" value="<?= $d ?>">
					<input type="hidden" name="o" value="<?= $o ?>">
					</form>
				<?

			break;

		}

	break;

	//-----------------------------------------------------------------
	//  Add Reply
	//-----------------------------------------------------------------

	case 5:

		//-----------------------------------------------------------------
		//  Start secondary switch
		//-----------------------------------------------------------------

		switch($c)
		{
			//-----------------------------------------------------------------
			//  Add the reply
			//-----------------------------------------------------------------

			case 1:

				//-----------------------------------------------------------------
				//  Start Checks
				//-----------------------------------------------------------------

				if(!isset($is_loged_in))
				{
					$id = NULL;
					if(!$username){ header("location: $PHP_SELF?n=modules/forum&a=5&d=$d&o=$o&q=$q&i=102"); exit; }
					$username = jcm_htmlspecialchars(do_censor($username));
					if(!$email){ header("location: $PHP_SELF?n=modules/forum&a=5&d=$d&o=$o&q=$q&i=103"); exit; }
					if(!eregi('^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.([a-zA-Z]{2,4})$', $email)){ header("location: $PHP_SELF?n=modules/forum&a=5&d=$d&o=$o&q=$q&i=104"); exit; }
					$author_reg = 1;
				}
				else
				{
					$user = $db->getLine("SELECT * FROM users WHERE id = '".$db->escape($_SESSION["$security_session"]['id'])."' AND username = '".$db->escape($_SESSION["$security_session"]['username'])."'");
					$id = $user['id'];
					$username = $user['username'];
					$email = $user['email'];
					$author_reg = 2;
				}

				if(!$sendmessage){ header("location: $PHP_SELF?n=modules/forum&a=5&d=$d&o=$o&q=$q&i=105"); exit; }

				//-----------------------------------------------------------------
				//  BBcode check
				//-----------------------------------------------------------------

				for ($i = 1; $i <= 5; $i++) if(check_tag_order($sendmessage) == $i) { header("location: $PHP_SELF?n=modules/forum&a=5&d=$d&o=$o&q=$q&i=9$i"); exit; }

				//-----------------------------------------------------------------
				//  Take care of some things
				//-----------------------------------------------------------------

				$sendmessage = jcm_linebreaks(trim(jcm_stripslashes($sendmessage)));
				if($overflow = check_tag_order($sendmessage)) $sendmessage = substr($sendmessage, 0, $overflow[0]).substr($sendmessage, $overflow[1], (strlen($sendmessage) - $overflow[0]));

				//-----------------------------------------------------------------
				//  Write in database
				//-----------------------------------------------------------------

				$db->insertRow("forum_replies",array('cat' => ''.$db->escape($d).'','subcat' => ''.$db->escape($o).'','topic' => ''.$db->escape($q).'','date' => ''.time().'','user_reg' => ''.$author_reg.'','user_id' => ''.$db->escape($id).'','user_user' => ''.$db->escape($username).'','user_email' => ''.$db->escape($email).'','message' => ''.$sendmessage.'','ip' => ''.find_ip().''));
				$check_reply = $db->getLine("SELECT * FROM forum_replies ORDER BY id DESC");
				$db->updateRow("forum_subcat",array('lastpost_location' => ''.$db->escape($check_reply['id']).'','lastpost_date' => ''.time().'','lastpost_reg' => ''.$db->escape($author_reg).'','lastpost_id' => ''.$db->escape($id).'','lastpost_user' => ''.$db->escape($username).'','lastpost_email' => ''.$db->escape($email).''),"id = '".$db->escape($o)."'");
				$db->updateRow("forum_topics",array('lastpost_location' => ''.$db->escape($check_reply['id']).'','lastpost_date' => ''.time().'','lastpost_reg' => ''.$db->escape($author_reg).'','lastpost_id' => ''.$db->escape($id).'','lastpost_user' => ''.$db->escape($username).'','lastpost_email' => ''.$db->escape($email).''),"id = '".$db->escape($q)."'");
				$check_topics = $db->getInfo_countRows("forum_topics","cat = '".$db->escape($d)."' AND subcat = '".$db->escape($o)."' AND type != '3'");
				$check_replies = $db->getInfo_countRows("forum_replies","cat = '".$db->escape($d)."' AND subcat = '".$db->escape($o)."'");
				$db->updateRow("forum_subcat",array('topics' => ''.$db->escape($check_topics).'', 'replies' => ''.$db->escape($check_replies).''),"id = '".$db->escape($o)."'");

				if($author_reg == "2")
				{
					$plus_forumposts = $user['forumposts'] + 1;
					$db->updateRow("users",array('forumposts' => ''.$db->escape($plus_forumposts).''),"id = '".$db->escape($user['id'])."'");
				}

				//-----------------------------------------------------------------
				//  Redirect when finished
				//-----------------------------------------------------------------

				header("location: $PHP_SELF?n=modules/forum&a=6&d={$check_reply['id']}");

			break;

			//-----------------------------------------------------------------
			//  View reply Form
			//-----------------------------------------------------------------

			default:

				//-----------------------------------------------------------------
				//  If user is guest and restriction is turned on
				//-----------------------------------------------------------------

				if($config['forum_guest_reply'] == "0" && isset($is_guest)){ header("location: $PHP_SELF?i=2"); exit; }

				//-----------------------------------------------------------------
				//  Breadcrumbs + Check for existance
				//-----------------------------------------------------------------

				$forum_cat = $db->getLine("SELECT * FROM forum_cat",$db->escape($d));
				if(!$forum_cat) header("location: $PHP_SELF?i=error");
				$forum_subcat = $db->getLine("SELECT * FROM forum_subcat",$db->escape($o));
				if(!$forum_subcat) header("location: $PHP_SELF?i=error");
				$forum_topic = $db->getLine("SELECT * FROM forum_topics",$db->escape($q));
				if(!$forum_topic) header("location: $PHP_SELF?i=error");

				echo "<table class='main' cellspacing='1' cellpadding='4'><tr class='con1'><td><a href='$PHP_SELF?n=modules/forum'>{$config['forum_name']}</a> &raquo; <a href='$PHP_SELF?n=modules/forum&a=1&d={$forum_cat['id']}'>{$forum_cat['name']}</a> &raquo; <a href='$PHP_SELF?n=modules/forum&a=2&d={$forum_cat['id']}&o={$forum_subcat['id']}'>{$forum_subcat['name']}</a> &raquo; <a href='$PHP_SELF?n=modules/forum&a=3&d={$forum_cat['id']}&o={$forum_subcat['id']}&q={$forum_topic['id']}'>{$forum_topic['name']}</a></td></tr></table>";

				//-----------------------------------------------------------------
				//  Start Restrictions
				//-----------------------------------------------------------------

				if($forum_subcat['type'] == "3" && isset($is_guest) || $forum_subcat['type'] == "3" && isset($is_user)){ header("location: $PHP_SELF?i=2"); exit; }
				if($forum_subcat['type'] == "4" && isset($is_guest) || $forum_subcat['type'] == "4" && isset($is_user)){ header("location: $PHP_SELF?i=2"); exit; }
				if($forum_subcat['type'] == "5") header("location: http://{$forum_subcat['redirect_location']}");

				if($forum_topic['mode'] == "1" && isset($is_guest) || $forum_topic['mode'] == "1" && isset($is_user) || $forum_topic['mode'] == "1" && isset($is_moderator)){ header("location: $PHP_SELF?i=2"); exit; }
				if($forum_topic['type'] == "2" && isset($is_guest) || $forum_topic['type'] == "2" && isset($is_user)){ header("location: $PHP_SELF?i=2"); exit; }
				if($forum_topic['mode'] == "2" && isset($is_guest) || $forum_topic['mode'] == "2" && isset($is_user)){ header("location: $PHP_SELF?i=2"); exit; }

				//-----------------------------------------------------------------
				//  Start Layout
				//-----------------------------------------------------------------

				$results = $db->getTable("SELECT r.id, r.user_id, r.user_user, r.user_email, r.message, r.date, u.id AS uid FROM forum_replies AS r LEFT JOIN users AS u ON r.user_id=u.id WHERE r.cat = '".$db->escape($d)."' AND r.subcat = '".$db->escape($o)."' AND r.topic = '".$db->escape($q)."' ORDER BY id DESC LIMIT 10");

				foreach($results as $idx=>$result)
				{
					if(isset($result['uid'])) $layout .= "<tr class='bottom'><td><span style='font-size: 12px; font-weight: bold;'><a href='$PHP_SELF?n=modules/users&a=1&d={$result['user_id']}'>{$result['user_user']}</a></span></td>";
					else $layout .= "<tr class='bottom'><td><span style='font-size: 12px; font-weight: bold;'><a href='mailto:{$result['user_email']}'>{$result['user_user']}</a></span></td>";

					$layout .= "<td><b><font color='#848484'><img src='templates/$template/images/buttons/date.gif' border='0' alt='{$language['Forum add reply desc10']}'> ".today_yesterday($result['date'])."</font></b></td></tr>";
					$layout .= "<tr class='con1'><td>&nbsp;</td><td>".parse_message($result['message'])."</td></tr>";
				}

				//-----------------------------------------------------------------
				//  View if reply is quote
				//-----------------------------------------------------------------

				if(isset($k))
				{
					$quote = $db->getLine("SELECT user_user, message FROM forum_replies WHERE id = '".$db->escape($k)."'");
					$reply_a = "[quote={$quote['user_user']}]{$quote['message']}[/quote]"; 
				}
				else $reply_a = NULL;

				//-----------------------------------------------------------------
				//  View The Form
				//-----------------------------------------------------------------

				?>
					<br>
					<form action="<?= $PHP_SELF ?>?n=modules/forum" method="post" name="textarea">
					<table class="main" cellspacing="1" cellpadding="4">
					<col width="25%"><col width="75%">
					<tr class="head"><td colspan="2"><?= $language['Forum add reply desc'] ?> [<?= $forum_topic['name'] ?>]</td></tr>
					<? if(!isset($is_loged_in)) { ?>
					<tr class="con1"><td><?= $language['Forum add reply desc2'] ?></td><td><input style="width:40%" maxlength="35" class="box" name="username"></td></tr>
					<tr class="con1"><td><?= $language['Forum add reply desc3'] ?></td><td><input style="width:40%" maxlength="35" class="box" name="email"></td></tr>
					<? } if($config['emoticons'] == 1) { ?>
					<tr class="con1">
					<td><?= $language['Forum add reply desc4'] ?></td>
					<td><?= insertSmilies(20) ?></td>
					</tr>
					<? } if($config['bbcode'] == 1) { ?>
					<tr class="con1">
					<td><?= $language['Forum add reply desc5'] ?></td>
					<td><?= insertBBcode(20) ?></td>
					</tr>
					<? } ?>
					<tr class="con1">
					<td valign="top"><?= $language['Forum add reply desc6'] ?><?= dis_bb_em(true) ?></td>
					<td><textarea style="width:100%; height:205px;" class="box" name="sendmessage"><?= $reply_a ?></textarea></td>
					</tr>
					<tr class="con1">
					<td><?= $language['Forum add reply desc7'] ?></td>
					<td><input type="submit" style="width:120" class="box" value="<?= $language['Forum add reply desc8'] ?>"></td>
					</tr>
					</table>
					<input type="hidden" name="a" value="5">
					<input type="hidden" name="c" value="1">
					<input type="hidden" name="d" value="<?= $d ?>">
					<input type="hidden" name="o" value="<?= $o ?>">
					<input type="hidden" name="q" value="<?= $q ?>">
					<input type="hidden" name="p" value="<?= $p ?>">
					</form>
					<br>
					<table class="main" cellspacing="1" cellpadding="4">
					<col width="25%"><col width="75%">
					<tr class="head"><td colspan="2"><?= $language['Forum add reply desc9'] ?></td></tr>
					<?= $layout ?>
					</table>
				<?

			break;
		}

	break;

	//-----------------------------------------------------------------
	//  Find Last reply
	//-----------------------------------------------------------------

	case 6:

		//-----------------------------------------------------------------
		//  Get Current Reply
		//-----------------------------------------------------------------

		$forum_reply = $db->getLine("SELECT cat, subcat, topic FROM forum_replies",$db->escape($d));

		//-----------------------------------------------------------------
		//  Calculate the Page
		//-----------------------------------------------------------------

		$numposts = $db->getInfo_countRows("forum_replies","cat = '".$db->escape($forum_reply['cat'])."' AND subcat = '".$db->escape($forum_reply['subcat'])."'");
		$p = ceil($numposts/$config['nav_replies']);

		//-----------------------------------------------------------------
		//  Redirect when finished
		//-----------------------------------------------------------------

		header("location: $PHP_SELF?n=modules/forum&a=3&d={$forum_reply['cat']}&o={$forum_reply['subcat']}&q={$forum_reply['topic']}&p=$p#$d");

	break;

	//-----------------------------------------------------------------
	//  Moderate Topics
	//-----------------------------------------------------------------

	case 7:

		//-----------------------------------------------------------------
		//  Filter Acces Rightes
		//-----------------------------------------------------------------

		if(isset($is_guest) || isset($is_user))
		{ header("location: $PHP_SELF?i=2"); exit; }

		//-----------------------------------------------------------------
		//  Start secondary switch
		//-----------------------------------------------------------------

		switch($c)
		{
			//-----------------------------------------------------------------
			//  Open/Lock/Move the topic
			//-----------------------------------------------------------------

			case 1:

				//-----------------------------------------------------------------
				//  Get Original
				//-----------------------------------------------------------------

				$forum_topic = $db->getLine("SELECT * FROM forum_topics",$db->escape($q));

				//-----------------------------------------------------------------
				//  Write in database
				//-----------------------------------------------------------------

				$db->updateRow("forum_topics",array('type' => ''.$db->escape($topic_type).''),"id = '".$db->escape($q)."'");

				//-----------------------------------------------------------------
				//  Write Log 
				//-----------------------------------------------------------------

				if($topic_type == 1) $topic_type_name = "Open";
				else $topic_type_name = "Locked";

				if($forum_topic['type'] != $topic_type)
					$db->insertRow("log",array('user_id' => ''.$db->escape($_SESSION["$security_session"]['id']).'','user_user' => ''.$db->escape($_SESSION["$security_session"]['username']).'','log' => ''.$db->escape($language['Logs desc2']).' <b>forum</b> the topic\'s type -> \''.$db->escape($forum_topic['name']).'\' to \''.$db->escape($topic_type_name).'\'','date' => ''.time().''));

				//-----------------------------------------------------------------
				//  Redirect when finished
				//-----------------------------------------------------------------

				header("location: $PHP_SELF?n=modules/forum&a=2&d=$d&o=$o&i=107");

			break;

			//-----------------------------------------------------------------
			//  Moderate the topic
			//-----------------------------------------------------------------

			case 2:

				//-----------------------------------------------------------------
				//  Get Original
				//-----------------------------------------------------------------

				$forum_topic = $db->getLine("SELECT * FROM forum_topics",$db->escape($q));

				//-----------------------------------------------------------------
				//  Start Checks
				//-----------------------------------------------------------------

				if(!$topic_name){ header("location: $PHP_SELF?n=modules/forum&a=7&d=$d&o=$o&q=$q&i=108"); exit; }
				$topic_name = jcm_stripslashes($topic_name);

				//-----------------------------------------------------------------
				//  Write in database
				//-----------------------------------------------------------------

				$db->updateRow("forum_topics",array('name' => ''.$db->escape($topic_name).'','mode' => ''.$db->escape($topic_mode).''),"id = '".$db->escape($q)."'");

				//-----------------------------------------------------------------
				//  Write Log 
				//-----------------------------------------------------------------

				if($topic_mode == 2) $topic_mode_name = "Sticky";
				else $topic_mode_name = "Normal";

				if($forum_topic['name'] != $topic_name) $changes = " -> '$topic_name'";
				else $changes = NULL;

				if($forum_topic['name'] != $topic_name)
					$db->insertRow("log",array('user_id' => ''.$db->escape($_SESSION["$security_session"]['id']).'','user_user' => ''.$db->escape($_SESSION["$security_session"]['username']).'','log' => ''.$db->escape($language['Logs desc2']).' <b>forum</b> the topic\'s name -> \''.$db->escape($forum_topic['name']).'\' to \''.$db->escape($topic_name).'\'','date' => ''.time().''));

				if($forum_topic['mode'] != $topic_mode)
					$db->insertRow("log",array('user_id' => ''.$db->escape($_SESSION["$security_session"]['id']).'','user_user' => ''.$db->escape($_SESSION["$security_session"]['username']).'','log' => ''.$db->escape($language['Logs desc2']).' <b>forum</b> the topic\'s mode -> \''.$db->escape($topic_name).'\' to \''.$db->escape($topic_mode_name).'\'','date' => ''.time().''));

				//-----------------------------------------------------------------
				//  Redirect when finished
				//-----------------------------------------------------------------

				header("location: $PHP_SELF?n=modules/forum&a=2&d=$d&o=$o&i=107");

			break;

			//-----------------------------------------------------------------
			//  Move the topic
			//-----------------------------------------------------------------

			case 3:
	
				//-----------------------------------------------------------------
				//  Write in database
				//-----------------------------------------------------------------

				$forum_subcat = $db->getLine("SELECT * FROM forum_subcat",$db->escape($topic_move));
				if(!$forum_subcat) header("location: $PHP_SELF?i=error");
				$forum_topic = $db->getLine("SELECT * FROM forum_topics",$db->escape($q));
				if(!$forum_topic) header("location: $PHP_SELF?i=error");
				$forum_replies = $db->getTable("SELECT * FROM forum_replies WHERE `topic` = '".$db->escape($forum_topic['id'])."'");
				$db->insertRow("forum_topics",array('cat' => ''.$db->escape($forum_subcat['cat']).'','subcat' => ''.$db->escape($topic_move).'','date' => ''.$db->escape($forum_topic['date']).'','name' => ''.$db->escape($forum_topic['name']).'','author_reg' => ''.$db->escape($forum_topic['author_reg']).'','author_id' => ''.$db->escape($forum_topic['author_id']).'','author_user' => ''.$db->escape($forum_topic['author_user']).'','author_email' => ''.$db->escape($forum_topic['author_email']).'','views' => ''.$db->escape($forum_topic['views']).'','lastpost_location' => ''.$db->escape($forum_topic['lastpost_location']).'','lastpost_date' => ''.$db->escape($forum_topic['lastpost_date']).'','lastpost_reg' => ''.$db->escape($forum_topic['lastpost_reg']).'','lastpost_id' => ''.$db->escape($forum_topic['lastpost_id']).'','lastpost_user' => ''.$db->escape($forum_topic['lastpost_user']).'','lastpost_email' => ''.$db->escape($forum_topic['lastpost_email']).'','type' => ''.$db->escape($forum_topic['type']).'','mode' => ''.$db->escape($forum_topic['mode']).'','ip' => ''.$db->escape($forum_topic['ip']).'')); 
				$forum_last_topic = $db->getLine("SELECT * FROM forum_topics WHERE cat = ".$db->escape($forum_subcat['cat'])." AND subcat = ".$db->escape($topic_move)." ORDER BY id DESC");
				$db->updateRow("forum_topics",array('type' => '3','redirect_cat' => ''.$db->escape($forum_subcat['cat']).'','redirect_subcat' => ''.$db->escape($topic_move).'','redirect_topic' => ''.$db->escape($forum_last_topic['id']).''),"id = '".$db->escape($q)."'");
				foreach($forum_replies as $idx=>$forum_reply) $db->updateRow("forum_replies",array('cat' => ''.$db->escape($forum_subcat['cat']).'','subcat' => ''.$db->escape($topic_move).'','topic' => ''.$db->escape($forum_last_topic['id']).''),"id = '".$db->escape($forum_reply['id'])."'");
				if($forum_topic['lastpost_date'] > $forum_subcat['lastpost_date']) $db->updateRow("forum_subcat",array('lastpost_location' => ''.$db->escape($forum_topic['lastpost_location']).'','lastpost_date' => ''.$db->escape($forum_topic['lastpost_date']).'','lastpost_reg' => ''.$db->escape($forum_topic['lastpost_reg']).'','lastpost_id' => ''.$db->escape($forum_topic['lastpost_id']).'','lastpost_user' => ''.$db->escape($forum_topic['lastpost_user']).'','lastpost_email' => ''.$db->escape($forum_topic['lastpost_email']).''),"id = '".$db->escape($topic_move)."'");
				$forum_last_last_topic = $db->getLine("SELECT * FROM forum_topics WHERE cat = '".$db->escape($d)."' AND subcat = '".$db->escape($o)."' AND type != '3' ORDER BY lastpost_date DESC");
				$db->updateRow("forum_subcat",array('lastpost_location' => ''.$db->escape($forum_last_last_topic['lastpost_location']).'','lastpost_date' => ''.$db->escape($forum_last_last_topic['lastpost_date']).'','lastpost_reg' => ''.$db->escape($forum_last_last_topic['lastpost_reg']).'','lastpost_id' => ''.$db->escape($forum_last_last_topic['lastpost_id']).'','lastpost_user' => ''.$db->escape($forum_last_last_topic['lastpost_user']).'','lastpost_email' => ''.$db->escape($forum_last_last_topic['lastpost_email']).''),"id = '".$db->escape($o)."'");
				$check_topics = $db->getInfo_countRows("forum_topics","cat = '".$db->escape($d)."' AND subcat = '".$db->escape($o)."' AND type != '3'");
				$check_replies = $db->getInfo_countRows("forum_replies","cat = '".$db->escape($d)."' AND subcat = '".$db->escape($o)."'");
				$db->updateRow("forum_subcat",array('topics' => ''.$db->escape($check_topics).'', 'replies' => ''.$db->escape($check_replies).''),"id = '".$db->escape($o)."'");
				$check_topics = $db->getInfo_countRows("forum_topics","subcat = '".$db->escape($topic_move)."' AND type != '3'");
				$check_replies = $db->getInfo_countRows("forum_replies","subcat = '".$db->escape($topic_move)."'");
				$db->updateRow("forum_subcat",array('topics' => ''.$db->escape($check_topics).'', 'replies' => ''.$db->escape($check_replies).''),"id = '".$db->escape($topic_move)."'");

				//-----------------------------------------------------------------
				//  Write Log 
				//-----------------------------------------------------------------

				$db->insertRow("log",array('user_id' => ''.$db->escape($_SESSION["$security_session"]['id']).'','user_user' => ''.$db->escape($_SESSION["$security_session"]['username']).'','log' => ''.$db->escape($language['Logs desc2']).' <b>forum</b> the topic -> \''.$db->escape($forum_topic['name']).'\' moved to \''.$db->escape($forum_subcat['name']).'\'','date' => ''.time().''));

				//-----------------------------------------------------------------
				//  Redirect when finished
				//-----------------------------------------------------------------

				header("location: $PHP_SELF?n=modules/forum&a=2&d={$forum_subcat['cat']}&o=$topic_move&i=107");

			break;

			//-----------------------------------------------------------------
			//  View moderate Form
			//-----------------------------------------------------------------

			default:

				//-----------------------------------------------------------------
				//  Breadcrumbs + Check for existance
				//-----------------------------------------------------------------

				$forum_cat = $db->getLine("SELECT * FROM forum_cat",$db->escape($d));
				if(!$forum_cat) header("location: $PHP_SELF?i=error");
				$forum_subcat = $db->getLine("SELECT * FROM forum_subcat",$db->escape($o));
				if(!$forum_subcat) header("location: $PHP_SELF?i=error");
				$forum_topic = $db->getLine("SELECT * FROM forum_topics",$db->escape($q));
				if(!$forum_topic) header("location: $PHP_SELF?i=error");

				echo "<table class='main' cellspacing='1' cellpadding='4'><tr class='con1'><td><a href='$PHP_SELF?n=modules/forum'>{$config['forum_name']}</a> &raquo; <a href='$PHP_SELF?n=modules/forum&a=1&d={$forum_cat['id']}'>{$forum_cat['name']}</a> &raquo; <a href='$PHP_SELF?n=modules/forum&a=2&d={$forum_cat['id']}&o={$forum_subcat['id']}'>{$forum_subcat['name']}</a> &raquo; <a href='$PHP_SELF?n=modules/forum&a=3&d={$forum_cat['id']}&o={$forum_subcat['id']}&q={$forum_topic['id']}'>{$forum_topic['name']}</a></td></tr></table>";

				//-----------------------------------------------------------------
				//  Load all available categories for 'move to..'
				//-----------------------------------------------------------------

				$forum_cats = $db->getTable("SELECT * FROM forum_cat ORDER BY succession ASC");

				//-----------------------------------------------------------------
				//  Start Restriction
				//-----------------------------------------------------------------

				if($forum_topic['mode'] == "1") header("location: $PHP_SELF?n=modules/forum&a=3&d=$d&o=$o&q=$q");

				//-----------------------------------------------------------------
				//  View The Form
				//-----------------------------------------------------------------

				?>
					<br>
					<form action="<?= $PHP_SELF ?>?n=modules/forum" method="post">
					<table class="main" cellspacing="1" cellpadding="4">
					<col width="25%"><col width="75%">
					<tr class="head"><td colspan="2"><?= $language['Forum mod topic desc'] ?> [<?= $forum_topic['name'] ?>]</td></tr>
					</td></tr>
					<tr class="con1"><td><?= $language['Forum mod topic desc2'] ?></td><td><select style="width:50%" class="box" name="topic_type"><? if($forum_topic['type'] == 1) echo "<option value='1' selected>{$language['Forum mod topic desc3']}</option><option value='2'>{$language['Forum mod topic desc4']}</option>"; if($forum_topic['type'] == 2) echo "<option value='1'>{$language['Forum mod topic desc3']}</option><option value='2' selected>{$language['Forum mod topic desc4']}</option>"; if($forum_topic['type'] == 3) echo "<option value='1'>{$language['Forum mod topic desc3']}</option><option value='2'>{$language['Forum mod topic desc4']}</option>" ?></select></td></tr>
					<tr class="con1">
					<td><?= $language['Forum mod topic desc5'] ?></td>
					<td><input type="submit" style="width:120" class="box" value="<?= $language['Forum mod topic desc6'] ?>"></td>
					</tr>
					</table>
					<input type="hidden" name="a" value="7">
					<input type="hidden" name="c" value="1">
					<input type="hidden" name="d" value="<?= $d ?>">
					<input type="hidden" name="o" value="<?= $o ?>">
					<input type="hidden" name="q" value="<?= $q ?>">
					</form>
					<br>
					<form action="<?= $PHP_SELF ?>?n=modules/forum" method="post">
					<table class="main" cellspacing="1" cellpadding="4">
					<col width="25%"><col width="75%">
					<tr class="head"><td colspan="2"><?= $language['Forum mod topic desc7'] ?> [<?= $forum_topic['name'] ?>]</td></tr>
					<tr class="con1"><td><?= $language['Forum mod topic desc8'] ?></td><td><input style="width:50%" maxlength="50" class="box" value="<?= $forum_topic['name'] ?>" name="topic_name"></td></tr>
					</td></tr>
					<tr class="con1"><td><?= $language['Forum mod topic desc9'] ?></td><td><select style="width:50%" class="box" name="topic_mode"><? if($forum_topic['mode'] == 3) echo "<option value='3' selected>{$language['Forum mod topic desc10']}</option><option value='2'>{$language['Forum mod topic desc11']}</option>"; if($forum_topic['mode'] == 1) echo "<option value='3'>{$language['Forum mod topic desc10']}</option><option value='2'>{$language['Forum mod topic desc11']}</option>"; if($forum_topic['mode'] == 2) echo "<option value='3'>{$language['Forum mod topic desc10']}</option><option value='2' selected>{$language['Forum mod topic desc11']}</option>" ?></select></td></tr>
					<tr class="con1">
					<td><?= $language['Forum mod topic desc12'] ?></td>
					<td><input type="submit" style="width:120" class="box" value="<?= $language['Forum mod topic desc13'] ?>"></td>
					</tr>
					</table>
					<input type="hidden" name="a" value="7">
					<input type="hidden" name="c" value="2">
					<input type="hidden" name="d" value="<?= $d ?>">
					<input type="hidden" name="o" value="<?= $o ?>">
					<input type="hidden" name="q" value="<?= $q ?>">
					</form>
					<br>
					<form action="<?= $PHP_SELF ?>?n=modules/forum" method="post">
					<table class="main" cellspacing="1" cellpadding="4">
					<col width="25%"><col width="60%"><col width="15%">
					<tr class="head"><td colspan="3"><?= $language['Forum mod topic desc14'] ?> [<?= $forum_topic['name'] ?>]</td></tr>
					</td></tr>
					<tr class="con1"><td><?= $language['Forum mod topic desc15'] ?></td><td><select style="width:100%" class="box" name="topic_move"><? foreach($forum_cats as $idx=>$forum_cat){ $forum_subcats_move = $db->getTable("SELECT * FROM forum_subcat WHERE cat = '".$db->escape($forum_cat['id'])."' ORDER BY succession ASC"); echo "<optgroup label='{$forum_cat['name']}'>"; foreach($forum_subcats_move as $idx=>$forum_subcat_move) if($forum_subcat_move['type'] != 5) if($forum_subcat_move['id'] != $forum_subcat['id']) echo "<option value='{$forum_subcat_move['id']}'>{$forum_subcat_move['name']}</option>"; echo "</optgroup>";} ?></select></td><td><input type="submit" style="width:100%" class="box" value="<?= $language['Forum mod topic desc16'] ?>"></td></tr>
					</table>
					<input type="hidden" name="a" value="7">
					<input type="hidden" name="c" value="3">
					<input type="hidden" name="d" value="<?= $d ?>">
					<input type="hidden" name="o" value="<?= $o ?>">
					<input type="hidden" name="q" value="<?= $q ?>">
					</form>
				<?

			break;
		}
	
	break;

	//-----------------------------------------------------------------
	//  Edit Replies
	//-----------------------------------------------------------------

	case 8:

		//-----------------------------------------------------------------
		//  Start secondary switch
		//-----------------------------------------------------------------

		switch($c)
		{
			//-----------------------------------------------------------------
			//  Edit the reply
			//-----------------------------------------------------------------

			case 1:

				//-----------------------------------------------------------------
				//  Start Checks
				//-----------------------------------------------------------------

				if(!$sendmessage){ header("location: $PHP_SELF?n=modules/forum&a=8&d=$d&o=$o&q=$q&k=$k&p=$p&i=109"); exit; }

				//-----------------------------------------------------------------
				//  BBcode check
				//-----------------------------------------------------------------

				for ($i = 1; $i <= 5; $i++) if(check_tag_order($sendmessage) == $i) { header("location: $PHP_SELF?n=modules/forum&a=8&d=$d&o=$o&q=$q&k=$k&p=$p&i=9$i"); exit; }

				//-----------------------------------------------------------------
				//  Take care of some things
				//-----------------------------------------------------------------

				$sendmessage = jcm_linebreaks(trim(jcm_stripslashes($sendmessage)));
				if($overflow = check_tag_order($sendmessage)) $sendmessage = substr($sendmessage, 0, $overflow[0]).substr($sendmessage, $overflow[1], (strlen($sendmessage) - $overflow[0]));

				//-----------------------------------------------------------------
				//  Write in database
				//-----------------------------------------------------------------

				$db->updateRow("forum_replies",array('message' => ''.$sendmessage.''),"id = '".$db->escape($k)."'");

				//-----------------------------------------------------------------
				//  Redirect when finished
				//-----------------------------------------------------------------

				header("location: $PHP_SELF?n=modules/forum&a=3&d=$d&o=$o&q=$q&p=$p&i=110#$k");

			break;

			//-----------------------------------------------------------------
			//  View edit Form
			//-----------------------------------------------------------------

			default:

				//-----------------------------------------------------------------
				//  Breadcrumbs + Check for existance
				//-----------------------------------------------------------------

				$forum_cat = $db->getLine("SELECT * FROM forum_cat",$db->escape($d));
				if(!$forum_cat) header("location: $PHP_SELF?i=error");
				$forum_subcat = $db->getLine("SELECT * FROM forum_subcat",$db->escape($o));
				if(!$forum_subcat) header("location: $PHP_SELF?i=error");
				$forum_topic = $db->getLine("SELECT * FROM forum_topics",$db->escape($q));
				if(!$forum_topic) header("location: $PHP_SELF?i=error");
				$forum_reply = $db->getLine("SELECT * FROM forum_replies",$db->escape($k));
				if(!$forum_reply) header("location: $PHP_SELF?i=error");

				echo "<table class='main' cellspacing='1' cellpadding='4'><tr class='con1'><td><a href='$PHP_SELF?n=modules/forum'>{$config['forum_name']}</a> &raquo; <a href='$PHP_SELF?n=modules/forum&a=1&d={$forum_cat['id']}'>{$forum_cat['name']}</a> &raquo; <a href='$PHP_SELF?n=modules/forum&a=2&d={$forum_cat['id']}&o={$forum_subcat['id']}'>{$forum_subcat['name']}</a> &raquo; <a href='$PHP_SELF?n=modules/forum&a=3&d={$forum_cat['id']}&o={$forum_subcat['id']}&q={$forum_topic['id']}'>{$forum_topic['name']}</a></td></tr></table>";

				//-----------------------------------------------------------------
				//  Start Restrictions
				//-----------------------------------------------------------------

				if(isset($is_guest)) { if($forum_reply['user_reg'] != "1") { header("location: $PHP_SELF?i=2"); exit; } if($forum_reply['ip'] != find_ip()) { header("location: $PHP_SELF?i=2"); exit; } }
				if(isset($is_user)) if($forum_reply['user_id'] != $_SESSION["$security_session"]['id'] && $forum_reply['user_user'] != $_SESSION["$security_session"]['username']){ header("location: $PHP_SELF?i=2"); exit; }
				if(isset($is_moderator)) { if($forum_topic['mode'] == "1") { header("location: $PHP_SELF?i=2"); exit; } $check_user = $db->getLine("SELECT * FROM users WHERE id = '".$db->escape($forum_reply['user_id'])."' AND username = '".$db->escape($forum_reply['user_user'])."'"); if($check_user['authorization'] == "3" || $check_user['authorization'] == "4") { header("location: $PHP_SELF?i=2"); exit; } }
				if(isset($is_administrator)) { $check_user = $db->getLine("SELECT * FROM users WHERE id = '".$db->escape($forum_reply['user_id'])."' AND username = '".$db->escape($forum_reply['user_user'])."'"); if($check_user['authorization'] == "4") { header("location: $PHP_SELF?i=2"); exit; } }

				//-----------------------------------------------------------------
				//  View The Form
				//-----------------------------------------------------------------

				?>
					<br>
					<form action="<?= $PHP_SELF ?>?n=modules/forum" method="post" name="textarea">
					<table class="main" cellspacing="1" cellpadding="4">
					<col width="25%"><col width="75%">
					<tr class="head"><td colspan="2"><?= $language['Forum edit reply desc'] ?> [<?= $forum_topic['name'] ?>]</td></tr>
					<? if($config['emoticons'] == 1) { ?>
					<tr class="con1">
					<td><?= $language['Forum edit reply desc2'] ?></td>
					<td><?= insertSmilies(20) ?></td>
					</tr>
					<? } if($config['bbcode'] == 1) { ?>
					<tr class="con1">
					<td><?= $language['Forum edit reply desc3'] ?></td>
					<td><?= insertBBcode(20) ?></td>
					</tr>
					<? } ?>
					<tr class="con1">
					<td valign="top"><?= $language['Forum edit reply desc4'] ?><?= dis_bb_em(true) ?></td>
					<td><textarea style="width:100%; height:205px;" class="box" name="sendmessage"><?= $forum_reply['message'] ?></textarea></td>
					</tr>
					<tr class="con1">
					<td><?= $language['Forum edit reply desc5'] ?></td>
					<td><input type="submit" style="width:120" class="box" value="<?= $language['Forum edit reply desc6'] ?>"></td>
					</tr>
					</table>
					<input type="hidden" name="a" value="8">
					<input type="hidden" name="c" value="1">
					<input type="hidden" name="d" value="<?= $d ?>">
					<input type="hidden" name="o" value="<?= $o ?>">
					<input type="hidden" name="q" value="<?= $q ?>">
					<input type="hidden" name="k" value="<?= $k ?>">
					<input type="hidden" name="p" value="<?= $p ?>">
					</form>
				<?

			break;
		}

	break;

	//-----------------------------------------------------------------
	//  Delete Replies
	//-----------------------------------------------------------------

	case 9:

		//-----------------------------------------------------------------
		//  Get subcat, topic and reply
		//-----------------------------------------------------------------

		$forum_subcat = $db->getLine("SELECT * FROM forum_subcat",$db->escape($o));
		if(!$forum_subcat) header("location: $PHP_SELF?i=error");
		$forum_topic = $db->getLine("SELECT * FROM forum_topics",$db->escape($q));
		if(!$forum_topic) header("location: $PHP_SELF?i=error");
		$forum_reply = $db->getLine("SELECT * FROM forum_replies",$db->escape($k));
		if(!$forum_reply) header("location: $PHP_SELF?i=error");

		//-----------------------------------------------------------------
		//  Start Restrictions
		//-----------------------------------------------------------------

		if(isset($is_guest)) { if($forum_reply['user_reg'] != "1") { header("location: $PHP_SELF?i=2"); exit; } if($forum_reply['ip'] != find_ip()) { header("location: $PHP_SELF?i=2"); exit; } }
		if(isset($is_user)) if($forum_reply['user_id'] != $_SESSION["$security_session"]['id'] && $forum_reply['user_user'] != $_SESSION["$security_session"]['username']){ header("location: $PHP_SELF?i=2"); exit; }
		if(isset($is_moderator)) { if($forum_topic['mode'] == "1") { header("location: $PHP_SELF?i=2"); exit; } $check_user = $db->getLine("SELECT * FROM users WHERE id = '".$db->escape($forum_reply['user_id'])."' AND username = '".$db->escape($forum_reply['user_user'])."'"); if($check_user['authorization'] == "3" || $check_user['authorization'] == "4") { header("location: $PHP_SELF?i=2"); exit; } }
		if(isset($is_administrator)) { $check_user = $db->getLine("SELECT * FROM users WHERE id = '".$db->escape($forum_reply['user_id'])."' AND username = '".$db->escape($forum_reply['user_user'])."'"); if($check_user['authorization'] == "4") { header("location: $PHP_SELF?i=2"); exit; } }

		//-----------------------------------------------------------------
		//  Write in database
		//-----------------------------------------------------------------

		$db->deleteRow("forum_replies","id = '".$db->escape($forum_reply['id'])."'");
		$forum_replies = $db->getTable("SELECT * FROM forum_replies WHERE subcat = '".$db->escape($o)."' AND topic = '".$db->escape($q)."'");
		foreach($forum_replies as $idx=>$forum_last_reply) {}
		$db->updateRow("forum_subcat",array('lastpost_location' => ''.$db->escape($forum_last_reply['id']).'','lastpost_date' => ''.$db->escape($forum_last_reply['date']).'','lastpost_reg' => ''.$db->escape($forum_last_reply['user_reg']).'','lastpost_id' => ''.$db->escape($forum_last_reply['user_id']).'','lastpost_user' => ''.$db->escape($forum_last_reply['user_user']).'','lastpost_email' => ''.$db->escape($forum_last_reply['user_email']).''),"id = '".$db->escape($o)."'");
		$db->updateRow("forum_topics",array('lastpost_location' => ''.$db->escape($forum_last_reply['id']).'','lastpost_date' => ''.$db->escape($forum_last_reply['date']).'','lastpost_reg' => ''.$db->escape($forum_last_reply['user_reg']).'','lastpost_id' => ''.$db->escape($forum_last_reply['user_id']).'','lastpost_user' => ''.$db->escape($forum_last_reply['user_user']).'','lastpost_email' => ''.$db->escape($forum_last_reply['user_email']).''),"id = '".$db->escape($q)."'");
		$check_topics = $db->getInfo_countRows("forum_topics","subcat = '".$db->escape($o)."' AND type != '3'");
		$check_replies = $db->getInfo_countRows("forum_replies","subcat = '".$db->escape($o)."'");
		$db->updateRow("forum_subcat",array('topics' => ''.$db->escape($check_topics).'', 'replies' => ''.$db->escape($check_replies).''),"id = '".$db->escape($o)."'");

		//-----------------------------------------------------------------
		//  Write Log 
		//-----------------------------------------------------------------

		$db->insertRow("log",array('user_id' => ''.$db->escape($_SESSION["$security_session"]['id']).'','user_user' => ''.$db->escape($_SESSION["$security_session"]['username']).'','log' => ''.$db->escape($language['Logs desc']).' <b>forum</b> the reply -> \''.$db->escape($k).'\' from the topic \''.$db->escape($forum_topic['name']).'\'','date' => ''.time().''));

		//-----------------------------------------------------------------
		//  Redirect when finished
		//-----------------------------------------------------------------

		header("location: $PHP_SELF?n=modules/forum&a=3&d=$d&o=$o&q=$q&p=$p&i=111");

	break;

	//-----------------------------------------------------------------
	//  Delete Topics
	//-----------------------------------------------------------------

	case 10:

		//-----------------------------------------------------------------
		//  Get subcat and topic
		//-----------------------------------------------------------------

		$forum_subcat = $db->getLine("SELECT * FROM forum_subcat",$db->escape($o));
		if(!$forum_subcat) header("location: $PHP_SELF?i=error");
		$forum_topic = $db->getLine("SELECT * FROM forum_topics",$db->escape($q));
		if(!$forum_topic) header("location: $PHP_SELF?i=error");

		//-----------------------------------------------------------------
		//  Start Restrictions
		//-----------------------------------------------------------------

		if(isset($is_guest)) { if($forum_topic['author_reg'] != "1") { header("location: $PHP_SELF?i=2"); exit; } if($forum_topic['ip'] != find_ip()) { header("location: $PHP_SELF?i=2"); exit; } }
		if(isset($is_user)) if($forum_topic['author_id'] != $_SESSION["$security_session"]['id'] && $forum_topic['author_user'] != $_SESSION["$security_session"]['username']){ header("location: $PHP_SELF?i=2"); exit; }
		if(isset($is_moderator)) { if($forum_topic['mode'] == "1") { header("location: $PHP_SELF?i=2"); exit; } $check_user = $db->getLine("SELECT * FROM users WHERE id = '".$db->escape($forum_topic['author_id'])."' AND username = '".$db->escape($forum_topic['author_user'])."'"); if($check_user['authorization'] == "3" || $check_user['authorization'] == "4") { header("location: $PHP_SELF?i=2"); exit; } }
		if(isset($is_administrator)) { $check_user = $db->getLine("SELECT * FROM users WHERE id = '".$db->escape($forum_topic['author_id'])."' AND username = '".$db->escape($forum_topic['author_user'])."'"); if($check_user['authorization'] == "4") { header("location: $PHP_SELF?i=2"); exit; } }

		//-----------------------------------------------------------------
		//  Write in database
		//-----------------------------------------------------------------

		for($i = 0; $i <= count($forum_topic); $i++) $db->deleteRow("forum_replies","topic = '".$db->escape($q)."'");
		$db->deleteRow("forum_topics","id = '".$db->escape($q)."'");
		$forum_moved_topic = $db->getLine("SELECT * FROM forum_topics WHERE redirect_topic = ".$db->escape($q)."");
		if($forum_moved_topic) $db->deleteRow("forum_topics","id = '".$db->escape($forum_moved_topic['id'])."'");
		$forum_topics = $db->getTable("SELECT * FROM forum_topics WHERE cat = '".$db->escape($d)."' AND subcat = '".$db->escape($o)."' AND mode != '2' ORDER BY lastpost_date");
		foreach($forum_topics as $idx=>$forum_last_topic) {}
		$db->updateRow("forum_subcat",array('lastpost_location' => ''.$db->escape($forum_last_topic['lastpost_location']).'','lastpost_date' => ''.$db->escape($forum_last_topic['lastpost_date']).'','lastpost_reg' => ''.$db->escape($forum_last_topic['lastpost_reg']).'','lastpost_id' => ''.$db->escape($forum_last_topic['lastpost_id']).'','lastpost_user' => ''.$db->escape($forum_last_topic['lastpost_user']).'','lastpost_email' => ''.$db->escape($forum_last_topic['lastpost_email']).''),"id = '".$db->escape($o)."'");
		$check_topics = $db->getInfo_countRows("forum_topics","subcat = '".$db->escape($o)."' AND type != '3'");
		$check_replies = $db->getInfo_countRows("forum_replies","subcat = '".$db->escape($o)."'");
		$db->updateRow("forum_subcat",array('topics' => ''.$db->escape($check_topics).'', 'replies' => ''.$db->escape($check_replies).''),"id = '".$db->escape($o)."'");

		//-----------------------------------------------------------------
		//  Write Log 
		//-----------------------------------------------------------------

		$db->insertRow("log",array('user_id' => ''.$db->escape($_SESSION["$security_session"]['id']).'','user_user' => ''.$db->escape($_SESSION["$security_session"]['username']).'','log' => ''.$db->escape($language['Logs desc']).' <b>forum</b> the topic -> \''.$db->escape($forum_topic['name']).'\'','date' => ''.time().''));

		//-----------------------------------------------------------------
		//  Redirect when finished
		//-----------------------------------------------------------------

		header("location: $PHP_SELF?n=modules/forum&a=2&d=$d&o=$o&i=112");

	break;

	//-----------------------------------------------------------------
	//  Show Forums
	//-----------------------------------------------------------------

	default:

		//-----------------------------------------------------------------
		//  Breadcrumbs
		//-----------------------------------------------------------------

		echo "<table class='main' cellspacing='1' cellpadding='4'><tr class='con1'><td>{$config['forum_name']}</td></tr></table><br>";

		//-----------------------------------------------------------------
		//  Load MYSQL Table #1
		//-----------------------------------------------------------------

		$results = $db->getTable("SELECT c.id AS cid, c.name AS cname, s.id AS sid, s.type, s.name, s.description, s.topics, s.replies, s.lastpost_date, s.lastpost_email, s.lastpost_user, s.lastpost_id, s.lastpost_location, s.lastpost_reg, u.id AS uid FROM forum_cat AS c LEFT JOIN forum_subcat AS s ON s.cat=c.id LEFT JOIN users AS u ON s.lastpost_id=u.id ORDER BY c.succession ASC, s.succession ASC");

		$cur_category = 0;
		$cat_count = 0;

		//-----------------------------------------------------------------
		//  Start Display #1
		//-----------------------------------------------------------------

		foreach($results as $idx=>$result)
		{
			//-----------------------------------------------------------------
			//  Separate categories for each subcategory
			//-----------------------------------------------------------------

			if($result['cid'] != $cur_category)
			{
				if ($cur_category != 0) echo "<tr class='con4'><td colspan='5'></td></tr></table><br>";

				$cat_count++;

				echo "<table class='main' cellspacing='1' cellpadding='4'><tr class='head'><td colspan='5'><img src='templates/$template/images/misc/nav_link.gif' border='0'> <a href='$PHP_SELF?n=modules/forum&a=1&d={$result['cid']}'>{$result['cname']}</a></td></tr>";
				echo "<tr class='bottom'><td width='54%' colspan='2'>{$language['Forum view forums desc']}</td><td width='8%' align='center'>{$language['Forum view forums desc2']}</td><td width='8%' align='center'>{$language['Forum view forums desc3']}</td><td width='30%'>{$language['Forum view forums desc4']}</td></tr>";

				$cur_category = $result['cid'];
			}

			//-----------------------------------------------------------------
			//  Show categories if any
			//-----------------------------------------------------------------

			if($result['sid'] != FALSE)
			{
				//-----------------------------------------------------------------
				//  Check see what indicator fits
				//-----------------------------------------------------------------

				if($result['type'] == "5") $forum_indicator = "<img src='templates/$template/images/buttons/redirected.gif' alt='{$language['Forum indicator desc9']}'>";
				else
				{
					if(@in_array(array("{$result['sid']}","{$result['lastpost_date']}"),$_SESSION["$security_session"]['subcat']) && !@in_array(array("{$result['sid']}","{$result['lastpost_date']}"),$_SESSION["$security_session"]['subcat_read']))
					{
						if($result['type'] == "1") $forum_indicator = "<img src='templates/$template/images/buttons/new.gif' alt='{$language['Forum indicator desc']}'>";
						if($result['type'] == "2") $forum_indicator = "<img src='templates/$template/images/buttons/news_new.gif' alt='{$language['Forum indicator desc2']}'>";
						if($result['type'] == "3") $forum_indicator = "<img src='templates/$template/images/buttons/admin_new.gif' alt='{$language['Forum indicator desc3']}'>";
						if($result['type'] == "4") $forum_indicator = "<img src='templates/$template/images/buttons/locked_new.gif' alt={$language['Forum indicator desc7']}>";
					}
					else
					{
						if($result['type'] == "1") $forum_indicator = "<img src='templates/$template/images/buttons/old.gif' alt='{$language['Forum indicator desc4']}'>";
						if($result['type'] == "2") $forum_indicator = "<img src='templates/$template/images/buttons/news_old.gif' alt='{$language['Forum indicator desc5']}'>";
						if($result['type'] == "3") $forum_indicator = "<img src='templates/$template/images/buttons/admin_old.gif' alt='{$language['Forum indicator desc6']}'>";
						if($result['type'] == "4") $forum_indicator = "<img src='templates/$template/images/buttons/locked_old.gif' alt='{$language['Forum indicator desc8']}'>";
					}
				}

				//-----------------------------------------------------------------
				//  Do some HTML
				//-----------------------------------------------------------------

				echo "<tr><td class='con1' align='center' width='6%'>$forum_indicator</td>";
				echo "<td class='con1'><b><a href='$PHP_SELF?n=modules/forum&a=2&d={$result['cid']}&o={$result['sid']}'>{$result['name']}</a></b><br>{$result['description']}</td>";
				echo "<td class='con2' align='center'>"; if($result['type'] == "5") echo "--"; else echo $result['topics']; echo "</td>";
				echo "<td class='con2' align='center'>"; if($result['type'] == "5") echo "--"; else echo $result['replies']; echo "</td>";
				echo "<td class='con2'>";

				if(!isset($result['redirect_hits'])) $result['redirect_hits'] = "0";

				if($result['type'] == "5") echo "{$language['Forum view forums desc7']} {$result['redirect_hits']}";
				else
				{
					if(!isset($result['lastpost_date'])) echo "--";
					else
					{
						echo "<img src='templates/$template/images/buttons/date.gif' border='0' alt='{$language['Forum view forums desc5']}'> ".today_yesterday($result['lastpost_date'])."<br>{$language['Forum view forums desc6']} ";

						if(isset($result['uid'])) echo "<a href='$PHP_SELF?n=modules/users&a=1&d={$result['lastpost_id']}'>{$result['lastpost_user']}</a>";
						else echo "<a href='mailto:{$result['lastpost_email']}'>{$result['lastpost_user']}</a>";

						echo " <a href='$PHP_SELF?n=modules/forum&a=6&d={$result['lastpost_location']}'><img src='templates/$template/images/buttons/last_post.gif' border='0' alt='{$language['Forum view forums desc4']}'></a>";
					}

					echo "</td></tr>";
				}
			}
		}

		if($cur_category > 0) echo "<tr class='con4'><td colspan='5'></td></tr></table><br>";

		//-----------------------------------------------------------------
		//  Check see if we have some forum categories
		//-----------------------------------------------------------------

		if(!$results) echo "<table class='main' cellspacing='1' cellpadding='4'><tr class='con1'><td>{$language['Forum view forums desc8']}</td></tr></table>";
		else
		{
			//-----------------------------------------------------------------
			//  Show Indicators
			//-----------------------------------------------------------------

			?>
				<table class="main" cellspacing="1" cellpadding="4">
				<tr><td class="con1" width="33%">
				<table width="100%" cellspacing="0" cellpadding="0">
				<tr><td width="10%" class="empty"><img src="templates/<?= $template ?>/images/buttons/new.gif" border="0" alt="<?= $language['Forum indicator desc'] ?>"></td><td width="90%" class="empty"><?= $language['Forum indicator desc'] ?></td></tr>
				</table>
				</td><td class="con1" width="33%">
				<table width="100%" cellspacing="0" cellpadding="0">
				<tr><td width="10%" class="empty"><img src="templates/<?= $template ?>/images/buttons/news_new.gif" border="0" alt="<?= $language['Forum indicator desc2'] ?>"></td><td width="90%" class="empty"><?= $language['Forum indicator desc2'] ?></td></tr>
				</table>
				</td><td class="con1" width="33%">
				<table width="100%" cellspacing="0" cellpadding="0">
				<tr><td width="10%" class="empty"><img src="templates/<?= $template ?>/images/buttons/admin_new.gif" border="0" alt="<?= $language['Forum indicator desc3'] ?>"></td><td width="90%" class="empty"><?= $language['Forum indicator desc3'] ?></td></tr>
				</table>
				</td></tr>
				<tr><td class="con1" width="33%">
				<table width="100%" cellspacing="0" cellpadding="0">
				<tr><td width="10%" class="empty"><img src="templates/<?= $template ?>/images/buttons/old.gif" border="0" alt="<?= $language['Forum indicator desc4'] ?>"></td><td width="90%" class="empty"><?= $language['Forum indicator desc4'] ?></td></tr>
				</table>
				</td><td class="con1" width="33%">
				<table width="100%" cellspacing="0" cellpadding="0">
				<tr><td width="10%" class="empty"><img src="templates/<?= $template ?>/images/buttons/news_old.gif" border="0" alt="<?= $language['Forum indicator desc5'] ?>"></td><td width="90%" class="empty"><?= $language['Forum indicator desc5'] ?></td></tr>
				</table>
				</td><td class="con1" width="33%">
				<table width="100%" cellspacing="0" cellpadding="0">
				<tr><td width="10%" class="empty"><img src="templates/<?= $template ?>/images/buttons/admin_old.gif" border="0" alt="<?= $language['Forum indicator desc6'] ?>"></td><td width="90%" class="empty"><?= $language['Forum indicator desc6'] ?></td></tr>
				</table>
				</td></tr>
				<tr><td class="con1" width="33%">
				<table width="100%" cellspacing="0" cellpadding="0">
				<tr><td width="10%" class="empty"><img src="templates/<?= $template ?>/images/buttons/locked_new.gif" border="0" alt="<?= $language['Forum indicator desc7'] ?>"></td><td width="90%" class="empty"><?= $language['Forum indicator desc7'] ?></td></tr>
				</table>
				</td><td class="con1" width="33%">
				<table width="100%" cellspacing="0" cellpadding="0">
				<tr><td width="10%" class="empty"><img src="templates/<?= $template ?>/images/buttons/locked_old.gif" border="0" alt="<?= $language['Forum indicator desc8'] ?>"></td><td width="90%" class="empty"><?= $language['Forum indicator desc8'] ?></td></tr>
				</table>
				</td><td class="con1" width="33%">
				<table width="100%" cellspacing="0" cellpadding="0">
				<tr><td width="10%" class="empty"><img src="templates/<?= $template ?>/images/buttons/redirected.gif" border="0" alt="<?= $language['Forum indicator desc9'] ?>"></td><td width="90%" class="empty"><?= $language['Forum indicator desc9'] ?></td></tr>
				</table>
				</td></tr>
				</table>
			<?
		}

	break;
}
?>